Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Created June 20, 2015 20:11
Show Gist options
  • Save davidallsopp/4438ff9c9d7baf370ded to your computer and use it in GitHub Desktop.
Save davidallsopp/4438ff9c9d7baf370ded to your computer and use it in GitHub Desktop.
Multi-line entry in GHCI
import Control.Monad.State
:{
let test :: State Int Int
test = do
put 3
modify (+1)
get
:}
print $ execState test 0
@davidallsopp
Copy link
Author

Type signature is optional here, but just to show how to include it.

One-line functions with type signatures can be done as:

let addTwo :: Int -> Int -> Int ; addTwo x y = x + y

@davidallsopp
Copy link
Author

One-liners with let in a do block are possible, but non-obvious - you have to wrap the let assignment in brackets:

let s :: State Int Int; s = do {x <- get; let {y = 1}; return x }

@davidallsopp
Copy link
Author

Another do-block example, with equivalent >>=

Prelude> [1,2,3] >>= \x -> [x,x] >>= \x -> [x+1]
[2,2,3,3,4,4]

Prelude> :{
Prelude| do
Prelude|   x <- [1,2,3]
Prelude|   [x,x]
Prelude| :}

[1,1,2,2,3,3]

Prelude> :{
Prelude| do
Prelude|   x <- [1,2,3]
Prelude|   y <- [x,x]
Prelude|   [y+1] -- or return (y+1)
Prelude| :}

[2,2,3,3,4,4]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment