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]

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

According to cons, None corresponds to the empty built-in list:

>>> 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


  • Support for the standard Python ordered collection types: i.e. list, tuple, Iterator, OrderedDict.
>>> from collections import OrderedDict
>>> cons(('a', 1), OrderedDict())
OrderedDict([('a', 1)])
  • Existing cons behavior is easy to change and new collections are straightforward to add through multipledispatch.
  • 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.

Source Distribution

cons-0.1.2.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

cons-0.1.2-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page