[Haskell-beginners] Use of interact

Francesco Ariis fa-ml at ariis.it
Sun Feb 10 22:59:32 UTC 2019


On Sun, Feb 10, 2019 at 09:59:54PM +0100, Michele Alzetta wrote:
> For hello_pure I tried this:
> 
> hello_pure :: Int -> String
> hello_pure n
> | n < 1 = ""
> | otherwise = "Hello World" ++ "\n" ++ hello_pure ( n - 1 )
> 

Very good!

> And it works, although
> ++ "\n" ++
> doesn't feel so elegant.

If you want, you can rewrite is as a one-liner like this:

    hp2 :: Int -> String
    hp2 n = unlines $ replicate n "Hello world"

-F


More information about the Beginners mailing list