
Hello everybody ! Could someone explain me exactly how this function works? elementAt_w'pf = (last .) . take . (+ 1) It's a posted solution to: 99 Haskell problems, problem 3. I'm having trouble with the "(last .) ." thing! Hope someone can help! Thank You! Gilberto

Near as I can tell, this is basically having to do with partial application
and the order of precedence for the (.) operator. A great way to look at
this stuff is using the :t command in GHCi and checking out the types
involved.
Prelude> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> :t last
last :: [a] -> a
Prelude> :t (last .)
(last .) :: (a -> [c]) -> a -> c
Looking back at the type for (.) and plugging in "[a]" in place of "b" and
"a" in place of "c" we get:
([b] -> b) -> (a -> [b]) -> a -> b
and since "last" takes the place of the first function we can reduce that
to:
(a -> [b]) -> a -> b
GHCi used "c" where we used "b" but you can clearly see the signatures are
identical other than that detail.
What we're left with is, (last .) is used to take a function from some type
"a" that returns a list of type "b", and a "a" value, and then returns the
last value from that list of "b" types.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe
Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
Hope someone can help!
Thank You!
Gilberto
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I have a follow up question on this one. Is code like this: elementAt_w'pf = (last .) . take . (+ 1) considered good haskell? If so, will this ever become easy to read? What do I do to practice reading this? More specifically, how would I know in mind head, before asking the type checker in GHCi to write this pipeline with the section (last .) instead of simply making the (wrong) pipe: elementAt_w'pf = last . take . (+ 1) I am used to using pipes in the unix shell, but in the shell there is always only one input and one output. There is no currying going on and so it is very clear what we are dealing with. How do I learn this? Thanks, Dimitri Em 30/04/14 09:53, Kyle Murphy escreveu:
Near as I can tell, this is basically having to do with partial application and the order of precedence for the (.) operator. A great way to look at this stuff is using the :t command in GHCi and checking out the types involved.
Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> :t last last :: [a] -> a
Prelude> :t (last .) (last .) :: (a -> [c]) -> a -> c
Looking back at the type for (.) and plugging in "[a]" in place of "b" and "a" in place of "c" we get: ([b] -> b) -> (a -> [b]) -> a -> b
and since "last" takes the place of the first function we can reduce that to: (a -> [b]) -> a -> b
GHCi used "c" where we used "b" but you can clearly see the signatures are identical other than that detail.
What we're left with is, (last .) is used to take a function from some type "a" that returns a list of type "b", and a "a" value, and then returns the last value from that list of "b" types.
-R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe
mailto:gilbertomelfe@gmail.com> wrote: Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
Hope someone can help!
Thank You!
Gilberto
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, Apr 30, 2014 at 11:25:58AM -0600, Dimitri DeFigueiredo wrote:
I have a follow up question on this one. Is code like this:
elementAt_w'pf = (last .) . take . (+ 1)
considered good haskell?
Uses of composition sections like (last .) is not very common; I would not consider it good haskell (though tastes may differ). On the other hand, point-free notation, used in moderation, is considered good Haskell style. For example, instead of foo x = bar (baz (quux x)) you should write foo = bar . baz . quux In this case I would certainly consider the second definition better style than the first.
If so, will this ever become easy to read? What do I do to practice reading this?
Just read and write lots of code. Try transforming things into and out of point-free notation as an exercise.
More specifically, how would I know in mind head, before asking the type checker in GHCi to write this pipeline with the section (last .) instead of simply making the (wrong) pipe:
elementAt_w'pf = last . take . (+ 1)
Simply by working out the types in your head. This becomes much easier with practice.
I am used to using pipes in the unix shell, but in the shell there is always only one input and one output. There is no currying going on and so it is very clear what we are dealing with. How do I learn this?
There is always only one input and one output in Haskell as well. The thing that makes it more complicated is that unlike in the shell, those inputs and outputs can themselves be functions. -Brent
Em 30/04/14 09:53, Kyle Murphy escreveu:
Near as I can tell, this is basically having to do with partial application and the order of precedence for the (.) operator. A great way to look at this stuff is using the :t command in GHCi and checking out the types involved.
Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> :t last last :: [a] -> a
Prelude> :t (last .) (last .) :: (a -> [c]) -> a -> c
Looking back at the type for (.) and plugging in "[a]" in place of "b" and "a" in place of "c" we get: ([b] -> b) -> (a -> [b]) -> a -> b
and since "last" takes the place of the first function we can reduce that to: (a -> [b]) -> a -> b
GHCi used "c" where we used "b" but you can clearly see the signatures are identical other than that detail.
What we're left with is, (last .) is used to take a function from some type "a" that returns a list of type "b", and a "a" value, and then returns the last value from that list of "b" types.
-R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe
mailto:gilbertomelfe@gmail.com> wrote: Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
Hope someone can help!
Thank You!
Gilberto
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks everyone. It seems I asked the follow-up question too soon. Michael's or David's proposed "reformulations" elementAt_w'pf n xs = last (take (n + 1) xs) or elementAt_w'pf n = last . take (n + 1) are so much easier to read than the original elementAt_w'pf = (last .) . take . (+ 1) that I will defer learning how to read the original version for now. It seems learning to quickly decipher that beast in one's head is not a prerequisite to being an effective haskell programmer. So, I'll just parse it at my slow pace for now. I think I can see how pointfree style (which I really like) can be overused sometimes. Thanks again. This list rocks! :-) Dimitri Em 30/04/14 12:15, Brent Yorgey escreveu:
On Wed, Apr 30, 2014 at 11:25:58AM -0600, Dimitri DeFigueiredo wrote:
I have a follow up question on this one. Is code like this:
elementAt_w'pf = (last .) . take . (+ 1)
considered good haskell? Uses of composition sections like (last .) is not very common; I would not consider it good haskell (though tastes may differ). On the other hand, point-free notation, used in moderation, is considered good Haskell style. For example, instead of
foo x = bar (baz (quux x))
you should write
foo = bar . baz . quux
In this case I would certainly consider the second definition better style than the first.
If so, will this ever become easy to read? What do I do to practice reading this? Just read and write lots of code. Try transforming things into and out of point-free notation as an exercise.
More specifically, how would I know in mind head, before asking the type checker in GHCi to write this pipeline with the section (last .) instead of simply making the (wrong) pipe:
elementAt_w'pf = last . take . (+ 1) Simply by working out the types in your head. This becomes much easier with practice.
I am used to using pipes in the unix shell, but in the shell there is always only one input and one output. There is no currying going on and so it is very clear what we are dealing with. How do I learn this? There is always only one input and one output in Haskell as well. The thing that makes it more complicated is that unlike in the shell, those inputs and outputs can themselves be functions.
-Brent
Em 30/04/14 09:53, Kyle Murphy escreveu:
Near as I can tell, this is basically having to do with partial application and the order of precedence for the (.) operator. A great way to look at this stuff is using the :t command in GHCi and checking out the types involved.
Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> :t last last :: [a] -> a
Prelude> :t (last .) (last .) :: (a -> [c]) -> a -> c
Looking back at the type for (.) and plugging in "[a]" in place of "b" and "a" in place of "c" we get: ([b] -> b) -> (a -> [b]) -> a -> b
and since "last" takes the place of the first function we can reduce that to: (a -> [b]) -> a -> b
GHCi used "c" where we used "b" but you can clearly see the signatures are identical other than that detail.
What we're left with is, (last .) is used to take a function from some type "a" that returns a list of type "b", and a "a" value, and then returns the last value from that list of "b" types.
-R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe
mailto:gilbertomelfe@gmail.com> wrote: Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
Hope someone can help!
Thank You!
Gilberto
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 04/30/2014 11:29 AM, Gilberto Melfe wrote:
Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
It's not your fault, that solution is retarded. Once you remove all of the intentional obfuscation, it looks like, elementAt_w'pf n xs = last (take (n + 1) xs) which is easy to understand. You can un-retard it step-by-step: elementAt_w'pf = (last .) . take . (+ 1) <=> elementAt_w'pf n = ((last .) . take . (+ 1)) n = (last .) ((take . (+ 1)) n) = (last .) (take (n + 1)) = last . (take (n + 1)) <=> elementAt_w'pf n xs = (last . (take (n + 1))) xs = last (take (n + 1) xs) All I've used above is the precedence of the function composition operator, and the fact that (f . g) x = f (g x).

On the point of it being "retarded" there's some merit to that in that it's
written in point-free (or as some joking call it, pointless) style. The
idea is to remove the explicit variables and so the functions are
re-arranged and composed such that all the variables come at the end of the
statement and you're left with nothing but a chain of functions at the
start (which can sometimes be easier to understand since it's just function
composition). The downside, as you've seen is that it often hurts
readability and so there are some that are strongly opposed to point-free
style. If used carefully, and things are broken into smaller easier to
understand chunks (via for instance let or where clauses) it can still be
readable even using point-free style, but it's really dependent on both the
skill of the person writing the statement, and the familiarity and skill of
the person reading the statement. When in doubt about legibility it's
better to avoid point-free style, in particular if you start having to do
strange things like apply the (.) operator to itself (E.G. "(f .) . g" or
similar) you're probably being to clever for your own good. I wouldn't go
so far as to say you should *never* use point-free style, but you should
seriously consider if you're trying to just be clever and show off, or if
it really is making the statement simpler to understand via more clearly
showing how the functions are composed.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 1:19 PM, Michael Orlitzky
On 04/30/2014 11:29 AM, Gilberto Melfe wrote:
Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
It's not your fault, that solution is retarded. Once you remove all of the intentional obfuscation, it looks like,
elementAt_w'pf n xs = last (take (n + 1) xs)
which is easy to understand. You can un-retard it step-by-step:
elementAt_w'pf = (last .) . take . (+ 1)
<=> elementAt_w'pf n = ((last .) . take . (+ 1)) n
= (last .) ((take . (+ 1)) n)
= (last .) (take (n + 1))
= last . (take (n + 1))
<=> elementAt_w'pf n xs = (last . (take (n + 1))) xs
= last (take (n + 1) xs)
All I've used above is the precedence of the function composition operator, and the fact that (f . g) x = f (g x). _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I think this one is most clear removing only one point:
elementAt_w'pf n = last . take (n + 1)
No line noise, simple composition of two functions. Extremes of point
free style are frequently harmful, but understanding it is worthwhile
and sensibly limited application can improve readability. I'm not
sure I'd call the example here "extreme" but it's certainly
approaching it (in terms of what I find easily readable).
On Wed, Apr 30, 2014 at 11:16 AM, Kyle Murphy
On the point of it being "retarded" there's some merit to that in that it's written in point-free (or as some joking call it, pointless) style. The idea is to remove the explicit variables and so the functions are re-arranged and composed such that all the variables come at the end of the statement and you're left with nothing but a chain of functions at the start (which can sometimes be easier to understand since it's just function composition). The downside, as you've seen is that it often hurts readability and so there are some that are strongly opposed to point-free style. If used carefully, and things are broken into smaller easier to understand chunks (via for instance let or where clauses) it can still be readable even using point-free style, but it's really dependent on both the skill of the person writing the statement, and the familiarity and skill of the person reading the statement. When in doubt about legibility it's better to avoid point-free style, in particular if you start having to do strange things like apply the (.) operator to itself (E.G. "(f .) . g" or similar) you're probably being to clever for your own good. I wouldn't go so far as to say you should *never* use point-free style, but you should seriously consider if you're trying to just be clever and show off, or if it really is making the statement simpler to understand via more clearly showing how the functions are composed.
-R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat.
On Wed, Apr 30, 2014 at 1:19 PM, Michael Orlitzky
wrote: On 04/30/2014 11:29 AM, Gilberto Melfe wrote:
Hello everybody !
Could someone explain me exactly how this function works?
elementAt_w'pf = (last .) . take . (+ 1)
It's a posted solution to: 99 Haskell problems, problem 3.
I'm having trouble with the "(last .) ." thing!
It's not your fault, that solution is retarded. Once you remove all of the intentional obfuscation, it looks like,
elementAt_w'pf n xs = last (take (n + 1) xs)
which is easy to understand. You can un-retard it step-by-step:
elementAt_w'pf = (last .) . take . (+ 1)
<=> elementAt_w'pf n = ((last .) . take . (+ 1)) n
= (last .) ((take . (+ 1)) n)
= (last .) (take (n + 1))
= last . (take (n + 1))
<=> elementAt_w'pf n xs = (last . (take (n + 1))) xs
= last (take (n + 1) xs)
All I've used above is the precedence of the function composition operator, and the fact that (f . g) x = f (g x). _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (6)
-
Brent Yorgey
-
David Thomas
-
Dimitri DeFigueiredo
-
Gilberto Melfe
-
Kyle Murphy
-
Michael Orlitzky