Skip to main content

An implementation of Lisp/Scheme-like cons in Python.

Project description

Build Status Coverage Status PyPI

Python cons

An implementation of cons in Python.

Usage and Design

The cons package attempts to emulate the semantics of Lisp/Scheme's cons as closely as possible while incorporating all the built-in Python sequence types:

>>> from cons import cons, car, cdr
>>> cons(1, [])

>>> cons(1, ())

>>> cons(1, [2, 3])
[1, 2, 3]

In general, cons is designed to work with types.

According to the cons package, None corresponds to the empty built-in list, as nil does in some Lisps:

>>> cons(1, None)

The cons package follows Scheme-like semantics for empty sequences:

>>> car([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ConsError: Not a cons pair

>>> cdr([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ConsError: Not a cons pair

By default, str types are not considered cons-pairs, although they are sequences:

>>> cons("a", "string")
ConsPair('a' 'a string')

This setting can be overridden and other types can be similarly excluded from consideration by registering classes with the abc-based classes MaybeCons and NonCons.


  • Built-in support for the standard Python ordered sequence types: i.e. list, tuple, Iterator, OrderedDict.
>>> from collections import OrderedDict
>>> cons(('a', 1), OrderedDict())
OrderedDict([('a', 1)])
  • Existing cons behavior can be changed and support for new collections can be added through the generic functions cons.core._car and cons.core._cdr.
  • Built-in support for unification.
>>> from unification import unify, reify, var
>>> unify([1, 2], cons(var('car'), var('cdr')), {})
{~car: 1, ~cdr: [2]}

>>> reify(cons(1, var('cdr')), {var('cdr'): [2, 3]})
[1, 2, 3]

>>> reify(cons(1, var('cdr')), {var('cdr'): None})


pip install cons

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cons, version 0.4.2
Filename, size File type Python version Upload date Hashes
Filename, size cons-0.4.2-py3-none-any.whl (6.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size cons-0.4.2.tar.gz (21.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page