change to wikibook?

I was wondering if someone could explain this error? I typed in the sample code from the Haskell Beginning Wikibook to define a function for absolute value. I have GHC 7.0.4 for OSX. The error went away when I put parentheses around the negative value, so did the Haskell interpreter think I was entering - and a second argument, namely 10, when the parentheses weren't there? Thank you! *Main> let abs x = if x < 0 then -x else x *Main> abs 5 5 *Main> abs -10 And got the following error <interactive>:1:6: No instance for (Num (a0 -> a0)) arising from the literal `10' Possible fix: add an instance declaration for (Num (a0 -> a0)) In the second argument of `(-)', namely `10' In the expression: abs - 10 In an equation for `it': it = abs - 10

On Sat, May 12, 2012 at 4:15 PM, Ashley Smith
I was wondering if someone could explain this error? I typed in the sample code from the Haskell Beginning Wikibook to define a function for absolute value. I have GHC 7.0.4 for OSX. The error went away when I put parentheses around the negative value, so did the Haskell interpreter think I was entering - and a second argument, namely 10, when the parentheses weren't there? Thank you!
If the WikiBook provided that as an example, then it's buggy and someone should fix it. This is a longstanding and difficult to fix annoyance of Haskel's syntax; the parenthese *are* required there. Briefly: unary operators don't work very well. It can't resolve them by context because it's not impossible for a function to typecheck there (nobody recommends Num instances for functions, but they're not forbidden either), and syntactically they conflict with sections (resolved in favor of the unary op, with the result that there's a function "subtract" that exists just to facilitate the (`subtract` 1) section.) What this adds up to is that you should be very careful around "-" because it's trying to satisfy too many mutually incompatible pieces of syntax. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Sat, May 12, 2012 at 10:15:42PM +0200, Ashley Smith wrote:
I was wondering if someone could explain this error? I typed in the sample code from the Haskell Beginning Wikibook to define a function for absolute value. I have GHC 7.0.4 for OSX. The error went away when I put parentheses around the negative value, so did the Haskell interpreter think I was entering - and a second argument, namely 10, when the parentheses weren't there? Thank you!
Exactly. This is a rather ugly corner of Haskell's lexical syntax, but it matches the official specification. Negative numbers often require parentheses around them so that the negation symbol does not parse as subtraction. If the wikibook itself actually lists "abs -10" as an example for you to try out, it should be fixed. -Brent
participants (4)
-
Ashley Smith
-
Brandon Allbery
-
Brent Yorgey
-
Gregg Lebovitz