Created
August 11, 2025 14:36
-
-
Save saadshahd/d468f545bbdcd2e9dc809057c5f083d7 to your computer and use it in GitHub Desktop.
McCarthy's eval in modern FP terms - 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
def eval(expr, env): | |
return match expr: | |
case Atom(x): Success(lookup(env, x)) | |
case List(['quote', x]): Success(x) | |
case List(['if', cond, then, else_]): | |
eval(cond, env).flatmap( | |
lambda v: eval(then if v else else_, env) | |
) | |
case List([fn, *args]): | |
eval(fn, env).flatmap( | |
lambda f: traverse(args, lambda a: eval(a, env)) | |
.flatmap(lambda vals: apply(f, vals)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment