[Haskell-cafe] loop problem

Mateusz Lenik mlen at mlen.pl
Sat May 10 15:01:24 UTC 2014


Hi,

There are several issues with the code:

- type annotation states that the function returns 'IO ()', while on the else
  branch 'IO Int' is returned. To fix it you should either change the returned
  type to 'IO Int' or replace 'return n' with 'return ()';
- in do notation you have to use let syntax, so 'x n = n * n' should be
  'let x n = n * n';
- this brings us to another issue in line 'putStrLn (show x n)', which is
  equivalent to 'putStrLn ((show x) n)'. You should put parentheses explicitly
  to fix it, or change x definition to 'let x = n * n', so that you can just
  write 'putStrLn (show x)'.

Cheers!
Mateusz

On Sat, May 10, 2014 at 04:32:56PM +0200, Roelof Wobben wrote:
>    Hello,
> 
>    I have to make a loop where on the number 1 - 100 the square is
>    calculated.
> 
>    So I did this :
> 
>    loop :: Int -> IO ()
>    loop n = do
>        if n <= 100
>        then do
>            x n = n * n;
>            putStrLn (show n );
>            putChar ' ';
>            putStrLn  (show x n);
>            loop (n + 1);
>        else
>           return n ;
> 
>    main :: IO ()
>    main = loop 1
> 
>    but now I see this error message :
> 
>      * Main.hs at 5:13-5:14
>        parse error on input `='
>      * *
>        undefine
>        :: IO ()
> 
>    So somthing is wrong at this line x n = n * n
>    But its looking well to me.
> 
>    Where did I take the wrong lane or were did I misunderstood something.
>    Give me a tip or a explanation please and not a answer.
>    Otherwise I do not learn anything .
> 
>    Roelof

> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


-- 
mlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140510/5963dd9d/attachment.sig>


More information about the Haskell-Cafe mailing list