strange take result--explanation?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

strange take result--explanation?

7stud-2
*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

Reply | Threaded
Open this post in threaded view
|

Re: strange take result--explanation?

7stud-2
7stud <bbxx789_05ss <at> 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?




Reply | Threaded
Open this post in threaded view
|

Re: strange take result--explanation?

Francesco Bochicchio
2009/3/12 7stud <[hidden email]>

> 7stud <bbxx789_05ss <at> 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
Reply | Threaded
Open this post in threaded view
|

Re: strange take result--explanation?

Francesco Bochicchio
>
>  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
Reply | Threaded
Open this post in threaded view
|

Re: strange take result--explanation?

Brent Yorgey-2
In reply to this post by 7stud-2
On Thu, Mar 12, 2009 at 08:14:41AM +0000, 7stud wrote:

> 7stud <bbxx789_05ss <at> 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?

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
Reply | Threaded
Open this post in threaded view
|

Re: strange take result--explanation?

Heinrich Apfelmus
In reply to this post by Francesco Bochicchio
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