[Haskell-beginners] $ versus .
Bob Ippolito
bob at redivi.com
Tue Jan 26 17:25:03 UTC 2021
The difference is that one of them is:
— unnecessary parentheses for emphasis
(init tail) [1, 2, 3]
And the other is
— parentheses required for correct evaluation
init (tail [1, 2, 3])
On Tue, Jan 26, 2021 at 08:23 Lawrence Bottorff <borgauf at gmail.com> wrote:
> > :t (init tail)
> : error:
> : * Couldn't match expected type `[a]'
> : with actual type `[a0] -> [a0]'
> : * Probable cause: `tail' is applied to too few arguments
> : In the first argument of `init', namely `tail'
> : In the expression: (init tail)
>
> > :t (init . tail)
> : (init . tail) :: [a] -> [a]
>
> > :t init $ tail
> : error:
> * Couldn't match expected type `[a]'
> with actual type `[a0] -> [a0]'
> * Probable cause: `tail' is applied to too few arguments
> In the second argument of `($)', namely `tail'
> In the expression: init $ tail
>
> > chopEnds = init $ tail
> > chopEnds [1,2,3]
> error: ...
> * Variable not in scope: chopEnds1 :: [Integer] -> t
> ...
>
> but then
>
> > init $ tail [1,2,3]
> [2]
>
> Not sure what I'm missing here. It doesn't make sense to me that the last
> expression works, but no version of a closure
>
> chopEnds = init $ tail
>
> does.
>
> On Mon, Jan 25, 2021 at 7:58 PM Kim-Ee Yeoh <ky3 at atamo.com> wrote:
>
>> init $ tail [1,2,3]
>> = init (tail ([1,2,3])) -- a la Lisp
>>
>> Now, functional programming is awesomest at abstractions. What if we
>> could abstract out "init (tail"?
>>
>> Then we could write
>>
>> chopEnds = init (tail
>>
>> But that looks weird. It's only got the left half of a parens pair!
>>
>> Does that explain why you should not expect the same result?
>>
>> A separate question is why the compiler even type-checks "init $ tail" in
>> the first place. What do you think is going on there?
>>
>> On Tue, Jan 26, 2021 at 1:16 AM Lawrence Bottorff <borgauf at gmail.com>
>> wrote:
>>
>>> I've got this
>>>
>>> > init $ tail [1,2,3]
>>> [2]
>>>
>>> and this
>>>
>>> > chopEnds = init $ tail
>>> > chopEnds [1,2,3]
>>> [1,2]
>>>
>>> What happened? Why is it not just init $ tail [1,2,3] ?
>>>
>>> This works fine
>>>
>>> > chopEnds2 = init . tail
>>> > chopEnds2 [1,2,3]
>>> [2]
>>>
>>> What am I missing?
>>>
>>> LB
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>> --
>> -- Kim-Ee
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210126/2b8a0b6d/attachment.html>
More information about the Beginners
mailing list