[Haskell-cafe] loop problem

Roelof Wobben r.wobben at home.nl
Sat May 10 16:11:41 UTC 2014


Stuart A. Kurtz schreef op 10-5-2014 18:02:
> Dear Roelof,
>
>
>> Thanks , it's working now like this :
>>
>> loop :: Int -> IO ()
>> loop n = do
>>      if n <= 100
>>      then do
>>          putStrLn (show n );
>>          putChar ' ';
>>          putStrLn (show (n * n )) ;
>>          loop (n + 1);
>>      else
>>         return () ;
>>
>> main :: IO ()
>> main = loop 1
>>
>> But the outcome looks like this:
>>
>> 1
>>     1
>> 2
>>     4
> You're getting good advice, but this strikes me as extraordinarily unidiomatic Haskell -- it reads like a translated C program. Fixing it doesn't eliminate that sense.
>
> Why not this?
>
>      main :: IO ()
>      main = putStr . unlines . map (unwords . map show . (\n -> [n,n^2])) $ [1..100]
>
> Peace,
>
> Stu
>
>

Like I said to another person . Im now following this course: 
https://www.fpcomplete.com/school/starting-with-haskell/basics-of-haskell

But I get the feeling it's not a good one. Maybe find another 
tutorial/course where I can learn it the right way and do a lot of 
exercises so I can see if I understand things right. I learn the best by 
doing things and not only read about it.

Roelof



More information about the Haskell-Cafe mailing list