[Haskell-cafe] How to print a string (lazily)
sebastian.sylvan at gmail.com
Tue Jan 3 12:59:15 EST 2006
On 1/3/06, Daniel Carrera <daniel.carrera at zmsl.com> wrote:
> I've been studying more Haskell and I've improved a lot. But I just hit
> a small problem. I want to print all the elements of a linst (putStr).
> I'd like to write something like this:
> print_list  = do putStr ""
> print_list (x:xs) = (do putStr x) && print_list xs
Others have already replied with a solution, but it looks to me like
what you're "missing" is how to sequence commands, which is the whole
purpose of the "do" notation.
print_list  = return ()
print_list (x:xs) =
do putStr x
The do notation is used here to sequence to IO actions (which answers
your second question), first it prints out the first character in the
string, then it calls itself recursively to print the rest of the
The empty list shouldn't print an empty string, it should do nothing
(that is, just return IO () because that's the return type of
More information about the Haskell-Cafe