io - How does getLine work in haskell? -


looking @ definition of getline in haskell prelude, how recursion works, keep asking character until hit newline , buildup list return wrapped in io.

however question how return statements work in case, how return (c:....:return "") work when hit base case. how cons return "" on list?

return isn't control structure in languages. it's constructor monadic values. let's take @ type:

return :: monad m => -> m 

in case, given string value, produces io string value.

the fact return last expression evaluated in each branch of if doesn't mean return ends execution; other expressions could occur after return. consider simple example list monad:

foo :: int -> int -> [int] foo x y = return x ++ return y 

in list monad, return creates new single-item list containing argument. 2 lists concatenated final result list returned function.

$ return 3 :: [int] [3] $ foo 3 4 [3,4] 

Comments