Skip to content

Instantly share code, notes, and snippets.

@TomLisankie
Created May 4, 2019 23:42
Show Gist options
  • Save TomLisankie/b89d3fad3e72bb670b11caf6d5b8448d to your computer and use it in GitHub Desktop.
Save TomLisankie/b89d3fad3e72bb670b11caf6d5b8448d to your computer and use it in GitHub Desktop.
Implementing Lisp `cons`, `car`, and `cdr` in Python
# Was watching this (https://www.youtube.com/watch?v=ymsbTVLbyN4&t=3749s) and Abelson was talking about how you could just make a cons cell with a lambda. Remembered Python had lambdas (although they're one-line lambdas smh) and figured "why not"
def cons(a, b):
return lambda x: a if (x == 1) else b
def car(cell):
return cell(1)
def cdr(cell):
return cell(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment