# strange take result--explanation?

6 messages
Open this post in threaded view
|

## strange take result--explanation?

 *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] :1:0:     No instance for (Num (Int -> [a] -> [a]))       arising from a use of `-' at :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] :1:6:     No instance for (Num ([t] -> Int -> [a] -> [a]))       arising from the literal `1' at :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
Open this post in threaded view
|

## Re: strange take result--explanation?

 7stud yahoo.com> 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?
Open this post in threaded view
|

## Re: strange take result--explanation?

 2009/3/12 7stud <[hidden email]> > 7stud yahoo.com> 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090312/596510f8/attachment.htm
Open this post in threaded view
|

## Re: strange take result--explanation?

 > >  But I don't understand because it says that 1 [1,2,3] is a single > argument... > Oh yes, because sintactically it looks like you want to apply the function '1' to the argument '[1,2,3]', the result being '1 [1,2,3]' Ciao again ----------- FB -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090312/6317ffea/attachment.htm