
Hi folks
let f = sum.map (^2).filter even f [1..4] 20
So far so good!
sum.map (^2).filter even [1..4] returns an error.
How is it that the composition above as part of a function equation doesn't return any errors while on its own it requires parentheses? I can understand the need for the parentheses - the composition operator takes two arguments/operand,s each of which is a mapping which must correspond to specific domain and range requirements i.e. (b->c -> (a->b) -> (a->c) Why aren't the parentheses required in the function definition? Cheers, Paul

On Apr 3, 2008, at 13:13 , PR Stanley wrote:
Hi folks
let f = sum.map (^2).filter even f [1..4] 20
So far so good!
sum.map (^2).filter even [1..4] returns an error.
How is it that the composition above as part of a function equation doesn't return any errors while on its own it requires parentheses?
You seem to have expression evaluation confused with cpp/m4 macros. The `f' in `f [1..4]' is not expanded as text to `sum.map (^2).filter even [1..4]'; it is evaluated as a function whose definition will (when needed! lazy language) eventually turn out to be `sum.map (^2).filter even'. If you really must view it as a text macro substitution, then imagine that every macro expansion has a set of parentheses added around it. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Hi folks
let f = sum.map (^2).filter even f [1..4] 20
So far so good!
sum.map (^2).filter even [1..4] returns an error.
How is it that the composition above as part of a function equation doesn't return any errors while on its own it requires parentheses? You seem to have expression evaluation confused with cpp/m4 macros. The `f' in `f [1..4]' is not expanded as text to `sum.map (^2).filter even [1..4]'; it is evaluated as a function whose definition will (when needed! lazy language) eventually turn out to be `sum.map (^2).filter even'. If you really must view it as a text macro substitution, then imagine that every macro expansion has a set of parentheses added around it. -- Okay, so it's not expanded like a CPP macro. How is it evaluated? What happens to the sum.map ... definition if it's not simply used to substitute f? Thanks, Paul

PR Stanley wrote: Just a meta-point. The dash-dash-space sequence introduces a signature. If you write your reply after the dash-dash-space, as you did here, a lot of us won't see your reply because we have our mail/news clients set up to ignore signatures. I had to view the original message source to see what was going on. As for the main point, you can look at it as a substitution; it's just that you have to jump through some hoops to think of it as substituting plain text. For example, put parentheses around the bit that's been substituted. A slightly more accurate model would be to view things as substitution of parse trees. Then one can point out that instead of doing straight substitution, what actually happens is that subexpressions are shared. But that only effects performance; the observable behavior is the same as if you did substitution (of parse trees, or of text but adding the parentheses in the right places).

PR Stanley
Hi folks
let f = sum.map (^2).filter even f [1..4] 20
So far so good!
sum.map (^2).filter even [1..4] returns an error.
How is it that the composition above as part of a function equation doesn't return any errors while on its own it requires parentheses? I can understand the need for the parentheses - the composition operator takes two arguments/operand,s each of which is a mapping which must correspond to specific domain and range requirements i.e. (b->c -> (a->b) -> (a->c) Why aren't the parentheses required in the function definition?
The answer is precedence. The (.) operator is right-associative and has lower precedence than function application, meaning that: a . b . c . d . e == a . (b . (c . (d . e))) so the second form is equivalent to: sum . ((map (^2)) . (filter even [1..4])) Typing :t (filter even [1..4]) should give you your explanation. If you want to avoid parentheses, you can write that expression as: sum . map (^2) . filter even $ [1..4] which really means (sum . map (^2) . filter even) [1..4] which is the same as "f [1..4]" above. Hope that helps, G
participants (4)
-
Brandon S. Allbery KF8NH
-
Chris Smith
-
Gregory Collins
-
PR Stanley