
On Sat, May 10, 2014, at 07:32 AM, Roelof Wobben wrote: So somthing is wrong at this line x n = n * n But its looking well to me. That line would be fine as a definition at the top level. But to nest a definition inside an expression, you need to introduce it with "let" or "where". -Karl

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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- mlen

Mateusz Lenik schreef op 10-5-2014 17:01:
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
Is it valid Haskell if I change the putStrln to putStrln ( show n * n) so I do not have to use the let command. Another question if I change return n with return () does the loop ever end. My feeling says not because the value of n is never updated for the loop. Roelof

On Sat, May 10, 2014 at 8:41 AM, Roelof Wobben
but now I see these errors :
- Main.hs@8:19-8:23 No instance for (Show (a0 -> a0)) arising from a use of `show' Possible fix: add an instance declaration for (Show (a0 -> a0)) In the first argument of `putStrLn', namely `(show x)' In a stmt of a 'do' block: putStrLn (show x) In the expression: do { let x n = n * n; putStrLn (show n); putChar ' '; putStrLn (show x); loop (n + 1) }
Think about what type x has.

On Sat, May 10, 2014 at 8:22 AM, Roelof Wobben
Is it valid Haskell if I change the putStrln to putStrln ( show n * n) so I do not have to use the let command.
You would need to write putStrLn (show (n * n)) in order for it to parse the way you intend. Function application is left-associative. You could also use print instead of putStrLn, which is shorthand for this. Another question if I change return n with return () does the loop ever
end. My feeling says not because the value of n is never updated for the loop.
Yes, the loop would terminate. The return n in your code is not updating n. The next iteration of the loop receives a new value of n as its argument due to your passing it in with loop (n + 1).

On 05/10/2014 05:53 PM, Roelof Wobben wrote:
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
Can I somehow make it like this
1 1 2 4 3 9
Roelof
Take a look at the `putStr' function. It omits the newline character that putStrLn writes at the end of the string. Jochem -- Jochem Berndsen | jochem@functor.nl

Hi Roelof, On 05/10/2014 05:22 PM, Roelof Wobben wrote:
Is it valid Haskell if I change the putStrln to putStrln ( show n * n) so I do not have to use the let command. Another question if I change return n with return () does the loop ever end. My feeling says not because the value of n is never updated for the loop.
Careful! show n * n is equal to (show n) * n, which is not what you wanted. Function application binds very strongly in Haskell. putStrLn (show (n*n)) is what you intended. In Haskell, you cannot 'update' values. The loop will terminate, however. Let's see why -- loop 101 will evaluate to return (). This is the action that, when performed, will do nothing. loop 100 will evaluate to do { putStrLn (show 100); putChar ' '; putStrLn (show (100 * 100)); loop (100 + 1) } This is equal to do { putStrLn (show 100); putChar ' '; putStrLn (show (100 * 100)); return () } So `loop 100' is equal to the action that, when performed, will print 100 10000 loop 99 will evaluate to do { putStrLn (show 99); putChar ' '; putStrLn (show (99 * 99)); loop (99 + 1) } which is equal to the action that, when performed, will print 99 9801 100 10000 and so on. There is only one way to actually perform an action, which is to put it into the definition of 'main'. To summarize, in Haskell there is a strict separation between the *evaluation* of values (including actions such as the above) and the *execution* of I/O, whereas most other languages conflate the two. For more information, see http://www.haskell.org/haskellwiki/Introduction_to_IO As an aside, the way this code is written is fairly unidiomatic. Haskell programmers in general like to separate I/O if possible. We'd generally write: values n = [(x, x*x) | x <- [1..n]] and then a function that writes general pairs of values. HTH, Jochem -- Jochem Berndsen | jochem@functor.nl

Jochem Berndsen schreef op 10-5-2014 17:53:
Hi Roelof,
On 05/10/2014 05:22 PM, Roelof Wobben wrote:
Is it valid Haskell if I change the putStrln to putStrln ( show n * n) so I do not have to use the let command. Another question if I change return n with return () does the loop ever end. My feeling says not because the value of n is never updated for the loop.
Careful! show n * n is equal to (show n) * n, which is not what you wanted. Function application binds very strongly in Haskell. putStrLn (show (n*n)) is what you intended.
In Haskell, you cannot 'update' values. The loop will terminate, however. Let's see why --
loop 101 will evaluate to return (). This is the action that, when performed, will do nothing.
loop 100 will evaluate to do { putStrLn (show 100); putChar ' '; putStrLn (show (100 * 100)); loop (100 + 1) }
This is equal to do { putStrLn (show 100); putChar ' '; putStrLn (show (100 * 100)); return () }
So `loop 100' is equal to the action that, when performed, will print 100 10000
loop 99 will evaluate to do { putStrLn (show 99); putChar ' '; putStrLn (show (99 * 99)); loop (99 + 1) } which is equal to the action that, when performed, will print 99 9801 100 10000
and so on.
There is only one way to actually perform an action, which is to put it into the definition of 'main'. To summarize, in Haskell there is a strict separation between the *evaluation* of values (including actions such as the above) and the *execution* of I/O, whereas most other languages conflate the two.
For more information, see http://www.haskell.org/haskellwiki/Introduction_to_IO
As an aside, the way this code is written is fairly unidiomatic. Haskell programmers in general like to separate I/O if possible. We'd generally write: values n = [(x, x*x) | x <- [1..n]] and then a function that writes general pairs of values.
HTH, Jochem
Oke, Im following this online course (https://www.fpcomplete.com/school/starting-with-haskell/basics-of-haskell) , Maybe I have to look at a better place to learn haskell the right way with a lot of exercises because I learn the best if I do it a lot and not only read about things. Roelof
participants (5)
-
Jochem Berndsen
-
Karl Voelker
-
Mateusz Lenik
-
Roelof Wobben
-
Taylor Hedberg