An even Easier Way to Define a Multiline Function in Haskell using GHCi
I previously blogged about Defining a multiline function in Haskell using Ghci. The solution was somewhat cumbersome and while hunting around for other options, I came across what I consider to be the simplest way to write a multiline function in GHCi.
From within GHCi, enclose your function between :{
and :}
strings.
For example:
:{
printAfter :: Int -> IO ()
= do
printAfter delay putStrLn $ (\d -> "waiting for " ++ d ++ " microseconds") $ show delay
Control.Concurrent.threadDelay delayputStrLn "done"
:}
If you can’t be bothered to add the opening and closing braces or you don’t need nice formatting, you can smash the whole function on one line by replacing every newline with a ;
:
printAfter :: Int -> IO (); printAfter delay = do; putStrLn $ (\d -> "waiting for " ++ d ++ " microseconds") $ show delay; Control.Concurrent.threadDelay delay;putStrLn "done"
And that’s it!