The `cons` library for Python provides an implementation of Lisp/Scheme-like cons cells, aiming to emulate their semantics while integrating with Python's built-in sequence types like lists and tuples. It facilitates functional programming patterns by allowing the construction and deconstruction of pairs. The library is actively maintained, with its current version being 0.4.7, and receives updates that include dependency bumps and Python version compatibility improvements.
pip install consVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create cons cells with various Python sequence types and how to extract their `car` (head) and `cdr` (tail) components. It also shows `None` being treated as an empty list (nil) for consing.
Upgrade Python environment to version 3.9 or higher.
Always ensure the argument to `car()` or `cdr()` is a valid cons pair or a sequence that has been created with `cons()` and is not empty. Check for emptiness before deconstructing.
If `str` type consing and deconstruction is desired, the library's behavior for considering types as `Cons`-pairs can be overridden by registering classes with `MaybeCons` and `NonCons` from `cons.core`.
Install the library using pip: `pip install cons`
Import the functions directly: `from cons import cons, car, cdr`
Ensure that `car` and `cdr` are called with valid `cons` objects or Python sequences that represent a cons structure: `car(cons(1, []))` or `car([1, 2, 3])`.
Provide both arguments to the `cons` function: `my_cons_cell = cons(1, [])` or `my_cons_cell = cons('a', ('b',))`