[Haskell-cafe] loop problem
mlen at mlen.pl
Sat May 10 15:01:24 UTC 2014
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)'.
On Sat, May 10, 2014 at 04:32:56PM +0200, Roelof Wobben wrote:
> I have to make a loop where on the number 1 - 100 the square is
> 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);
> 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 `='
> * *
> :: 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 .
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 833 bytes
Desc: not available
More information about the Haskell-Cafe