Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created November 27, 2009 00:21
Show Gist options
  • Save NicolasT/243732 to your computer and use it in GitHub Desktop.
Save NicolasT/243732 to your computer and use it in GitHub Desktop.
# State is represented as (value, state)
returnST = lambda a: lambda s: (a, s)
# The 'ss' thing is some trick to get around the inability to assign in a
# lambda expression, and get around calculating x(s) twice
bindST = lambda x: lambda f: lambda s: \
(lambda ss: runST(f(ss[0]))(ss[1]))(x(s))
runST = lambda f: lambda s: f(s)
getST = lambda s: (s, s)
putST = lambda a: lambda _: ((), a)
fun1 = bindST(getST)(lambda i: returnST(i * 2))
fun2 = bindST(fun1)(lambda i: putST(i))
print runST(fun2)(10)
# Returns ((), 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment