Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-
- ...but what if we need state?
- How would we write this imperative pseudocode in Haskell without the IO monad or any other monad?
- x = 1
- y = 2
- x = x + y comment: x == 3
- y = x * y comment: y == 3 * 2 = 6
- result = x + y comment: 3 + 6 = 9
- -}
- type LineLabel = Int
- type Program t = LineLabel -> t
- p :: Program (Int -> Int -> Int -> Int)
- p 0 x y r = p 1 1 y r -- x = 1
- p 1 x y r = p 2 x 2 r -- y = 2
- p 2 x y r = p 3 (x + y) y r -- x = x + y
- p 3 x y r = p 4 x (x * y) r -- y = x * y
- p 4 x y r = p 5 x y (x + y) -- r = x + y
- p 5 x y r = r
- program :: Int
- program = p 0 0 0 0
- -- program == 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement