
*Main> let x = [] *Main> take (length x - 1) [1, 2, 3] [] *Main> length x 0 *Main> take (0 - 1) [1, 2, 3] [] *Main> take -1 [1, 2, 3] <interactive>:1:0: No instance for (Num (Int -> [a] -> [a])) arising from a use of `-' at <interactive>:1:0-16 Possible fix: add an instance declaration for (Num (Int -> [a] -> [a])) In the expression: take - 1 [1, 2, 3] In the definition of `it': it = take - 1 [1, 2, 3] <interactive>:1:6: No instance for (Num ([t] -> Int -> [a] -> [a])) arising from the literal `1' at <interactive>:1:6-16 Possible fix: add an instance declaration for (Num ([t] -> Int -> [a] -> [a])) In the second argument of `(-)', namely `1 [1, 2, 3]' In the expression: take - 1 [1, 2, 3] In the definition of `it': it = take - 1 [1, 2, 3] *Main> take - 1 [1, 2, 3] ----- Why does take (0 - 1) [1, 2, 3] produce a result but not take -1 [1, 2, 3] ? Thanks

2009/3/12 7stud
7stud
writes: Why does
take (0 - 1) [1, 2, 3]
produce a result but not
take -1 [1, 2, 3]
? Thanks
Well, immediately after I hit the submit button, I thought I'd try this:
*Main> take (-1) [1, 2, 3] []
So why are the parentheses needed there?
I think because take -1 [1,2,3] is parsed as (take - 1 ) [1,2,3] or something like that. If you look to the error message, and translate the haskellese in plain english, it says so. "In the second argument of `(-)', namely `1 [1, 2, 3]' " : so it looks like is looking for the two arguments of infix operator (-), the first being 'take'. But I don't understand because it says that 1 [1,2,3] is a single argument... "In the definition of `it': it = take - 1 [1, 2, 3]" notice the blank between the minus sign and 1: even if you write -1, it understands - 1. So, ghc is trying to be helpful here :-) Ciao ----- FB

Francesco Bochicchio wrote:
But I don't understand because it says that 1 [1,2,3] is a single argument...
Oh yes, because syntactically it looks like you want to apply the function '1' to the argument '[1,2,3]', the result being '1 [1,2,3]'
Yes, the error messages indicate that GHC is parsing the expression as (take) - (1 [1,2,3]) and consequently complains that take :: Int -> [a] -> [a] is not a number (i.e. an instance of class Num) and that it can't interpret the inferred type of 1 :: [t] -> (type of take) as a Num . Regards, apfelmus -- http://apfelmus.nfshost.com

On Thu, Mar 12, 2009 at 08:14:41AM +0000, 7stud wrote:
7stud
writes: Why does
take (0 - 1) [1, 2, 3]
produce a result but not
take -1 [1, 2, 3]
? Thanks
Well, immediately after I hit the submit button, I thought I'd try this:
*Main> take (-1) [1, 2, 3] []
So why are the parentheses needed there?
Negative numbers are a rather ugly corner of the Haskell lexical specification. Indeed, without the parentheses, Haskell thinks you are trying to use subtraction. Just always use parentheses around negative numbers and you'll be fine. =) -Brent
participants (4)
-
7stud
-
Brent Yorgey
-
Francesco Bochicchio
-
Heinrich Apfelmus