Created
November 27, 2009 00:21
-
-
Save NicolasT/243732 to your computer and use it in GitHub Desktop.
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
# 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