[Haskell-cafe] How to print a string (lazily)

Neil Mitchell ndmitchell at gmail.com
Tue Jan 3 12:49:07 EST 2006


Hi,

All Haskell functions are lazy, hence there is no need to "write a
lazy version" of your print_list function. I think the function you
probably want is:

putStr (unlines xs)

This uses the bulid in unlines function, which is similar in spirit to
join (you get more quotes, which I guess you don't want)

The equivalent in monad'y programming is:

mapM putStrLn xs

The first one has fewer monads, so I prefer it, but take your pick :)

Thanks

Neil


On 1/3/06, Daniel Carrera <daniel.carrera at zmsl.com> wrote:
> Hello,
>
> 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
>
> I know this is wrong, but I hope you can see what I'm trying to do.
>
> I know of other ways I could print a list. For example:
>
> 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. It's
> not that I need such a function, I'm just trying to learn Haskell.
>
> Any suggestions?
>
> Question: What do you call a function that has side-effects? (like
> putStr) I know that "function" is the wrong term.
>
> Cheers,
> Daniel.
> --
>       /\/`) http://oooauthors.org
>      /\/_/  http://opendocumentfellowship.org
>     /\/_/
>     \/_/    I am not over-weight, I am under-tall.
>     /
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list