[Haskell-cafe] How to print a string (lazily)
u.stenzel at web.de
Tue Jan 3 16:28:54 EST 2006
Daniel Carrera 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 ""
This looks as if you're confused. The keyword "do" is completely
redundant. "do" does not mean "please ignore all rules and allow side
effects", it rather means "please build a new action by sequencing what
follows". So "do" with only one action after it is useless (and a sign
of confusion). Have you ever heard about the "command" and "composite"
design patterns? Well, in Haskell, you get them for free with monads.
> print_list (x:xs) = (do putStr x) && print_list xs
print_list (x:xs) = do putStr x ; print_list xs
> print_list xs = do putStr(join xs)
> where join  = ""
> join (x:xs) = (show x) ++ "\n" ++ join xs
> But the thing is, I want to write a lazy version of this function.
What's not lazy about it? If you think, join has to "complete" "before"
putStr can be called, simply forget it. Those words don't even make
sense in a lazy functional setting.
Lost: gray and white female cat. Answers to electric can opener.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060103/c1161652/attachment.bin
More information about the Haskell-Cafe