A toy example but still frustrating:
numberMapper:: IO ()
numberMapper = do codes <- forM [1 .. 4] (\num ->
do putStrLn $ "Enter a code for " ++ show num
code <- getLine
return code)
let numberCodes = zip [1 .. 4] codes
in forM numberCodes (\(num,code) ->
putStrLn $ "Got code " ++ show code ++ " for " ++ show num)
ghci tells me I have a Parse error in pattern: putStrLn and I can’t figure out why it should fail to parse.
Correction:
Fix: The lines inside a
doblock should line up.Fix 2: When using
letinside adoblock, don’t usein.Fix 3: Use
forM_(which returns(), a.k.a. void) instead offorM(which returns a list).So
forM_could (almost) be written like this:Minor change: You don’t need
returnhere:You can change it to the equivalent,