[Haskell-beginners] $ versus .
Lawrence Bottorff
borgauf at gmail.com
Tue Jan 26 16:23:12 UTC 2021
> :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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210126/70035314/attachment.html>
More information about the Beginners
mailing list