Created
May 4, 2019 23:42
-
-
Save TomLisankie/b89d3fad3e72bb670b11caf6d5b8448d to your computer and use it in GitHub Desktop.
Implementing Lisp `cons`, `car`, and `cdr` in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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