[Haskell-beginners] Problem with defining functions and guarded rules
Mike Meyer
mwm at mired.org
Mon Jul 25 09:02:20 CEST 2011
On Mon, 25 Jul 2011 05:38:40 +0000
This is still my display name <xning_90 at hotmail.com> wrote:
>
> Hi, I just started learning Haskell. But I have been having a problem with defining functions. For example,diff :: Float-> Float diff = if x>= y then x-y else y-x<interactive>:1:29: parse error on input `='It is rather weird as I have checked quite a few websites and this was supposed to work.The same occurs when I try for guarded rules, except that it is now for |.diff :: Float -> Float -> Floatdiff x y | x >= y = x - y| otherwise = y - x<interactive>:1:42: parse error on input `|'Please do help!Thanks a lot.
You're typing at GHCi, right? You can't create values at the top level
in ghci. You need to either put the things in a file and load them, or
use "let "
Prelude> diff = if x > y then x - y else y - x
<interactive>:1:6: parse error on input `='
Prelude> let diff = if x > y then x - y else y - x
<interactive>:1:15: Not in scope: `x'
<interactive>:1:19: Not in scope: `y'
<interactive>:1:26: Not in scope: `x'
<interactive>:1:30: Not in scope: `y'
<interactive>:1:37: Not in scope: `y'
<interactive>:1:41: Not in scope: `x'
Prelude> let diff x y = if x > y then x - y else y - x
Prelude> diff 20 10
10
Prelude> diff 10 20
10
Prelude>
--
Mike Meyer <mwm at mired.org> http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
More information about the Beginners
mailing list