
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

I think what you're missing is what you actually typed in the first case.
This is a type error, it will not compile or run:
chopEnds = init $ tail
The $ operator can always be rewritten as parentheses, in this case:
chopEnds = init (tail)
Which has the same incorrectly typed meaning as:
chopEnds = init tail
The "result" you pasted looks equivalent to:
chopEnds = init
Perhaps this is what you typed? In this case the argument tail it will
shadow the existing binding of the Prelude tail, which would be confusing
so with -Wall it would issue a compiler warning:
chopEnds tail = init $ tail
<interactive>:2:10: warning: [-Wname-shadowing]
This binding for ‘tail’ shadows the existing binding
imported from ‘Prelude’ (and originally defined in ‘GHC.List’)
On Mon, Jan 25, 2021 at 10:16 AM Lawrence Bottorff
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- -- Kim-Ee

: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
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
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- -- Kim-Ee _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
: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
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
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- -- Kim-Ee _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Bob Ippolito
-
Kim-Ee Yeoh
-
Lawrence Bottorff