1 Exercises for the enthusiast beginner
1.1 A simple exercise, read carefullyThis is a definition for the
function. If you do not know the bind operator
yet, do not worry about it.
behaves in a way similar to
p >> q
, but it involves side effects. Anyway the error you are asked to find is not related with it.
> readln = h > where h = getChar >>= f > where f c = case c of > '\n' -> return  > otherwise -> h >>= (return . (:) c)
Main> readln >>= print
It works, as expected.Now
is a higher order function obtained by the parameterization of
, it generalizes its behaviour. The new function
with the original parameters. It can be checked by replacing the parameters in the body of the function.
> readln' = hofun getChar '\n' (:)  > hofun r s op e = h > where h = r >>= f > where f c = case c of > s -> return e > otherwise -> h >>= (return . op c)
Main> readln' >>= print
does not works printing the input line as
readln' >>= print
does, there is an error, easy to find for the experienced eye. Can you discover it?
readln >>= print