Skip to content

Instantly share code, notes, and snippets.

@darius
Created September 16, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darius/cf46925bbb4f4f3bf38e6abdb1444573 to your computer and use it in GitHub Desktop.
Save darius/cf46925bbb4f4f3bf38e6abdb1444573 to your computer and use it in GitHub Desktop.
# h/t Dave Long
from collections import namedtuple
data = lambda s: {ws[0]: namedtuple(ws[0], ws[1:])
for term in s.split('|') for ws in [term.split()]}
Data = lambda s: globals().update(data(s))
Data("Nil | Cons head tail")
Nil .length = lambda xs: 0
Cons.length = lambda xs: 1 + xs.tail.length()
## Cons(42, Cons(137, Nil())).length()
#. 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment