In a *lot* of places in my programs, I am using notation f $ g $ h x in favour of f (g (h x)) (that's the '$' as defined in the Prelude: right-associating infix application operator) as it avoids parentheses, and makes the code more manageable (you can write "(upward) pipes" with one "$ f" per line, etc) I liked to think of it as just a syntactical convention (for years ...) but is it really at no cost? It does introduce extra function calls, that is, extra closures etc.? Can these be removed by ghc's optimizer? Or am I totally wrong here ... Actually, looking at the Prelude (now), there is '$!' as well - is that supposed to answer my question? -- -- Johannes Waldmann, Tel/Fax: (0341) 3076 6479 / 6480 -- ------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------
At 15:55 13/10/04 +0200, Johannes Waldmann wrote:
In a *lot* of places in my programs, I am using notation f $ g $ h x in favour of f (g (h x)) (that's the '$' as defined in the Prelude: right-associating infix application operator) as it avoids parentheses, and makes the code more manageable (you can write "(upward) pipes" with one "$ f" per line, etc)
I liked to think of it as just a syntactical convention (for years ...) but is it really at no cost? It does introduce extra function calls, that is, extra closures etc.? Can these be removed by ghc's optimizer?
I don't think it's *just* a syntactic convention, but in your case above I think they both yield the same function graph for evaluation so I see no cost there. Here's a little $-based idiom I rather like, which shows $ as more than just a no-op: [[ -- |Apply list of functions to some value, returning list of results. -- It's kind of like an converse map. -- -- This is similar to the 'ap' function in the Monad library. -- flist :: [a->b] -> a -> [b] flist fs a = map ($ a) fs ]] I suppose you could say that $ is a kind of syntactic convention, because without it juxtaposition for function application lacks the lexical visibility for expression in different syntactic contexts.
Or am I totally wrong here ... Actually, looking at the Prelude (now), there is '$!' as well - is that supposed to answer my question?
Well, that's a different function, I think. #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact
"Jacques Carette" <carette@mcmaster.ca> writes:
-- |Apply list of functions to some value, returning list of results. -- It's kind of like an converse map. flist :: [a->b] -> a -> [b] flist fs a = map ($ a) fs
I have attempted, unsuccessfully, to write flist above in a point-free manner. Is it possible?
flist = flip (map . (flip ($)))
On Wed, 13 Oct 2004 18:06:01 +0100, Malcolm Wallace <malcolm.wallace@cs.york.ac.uk> wrote:
"Jacques Carette" <carette@mcmaster.ca> writes:
-- |Apply list of functions to some value, returning list of results. -- It's kind of like an converse map. flist :: [a->b] -> a -> [b] flist fs a = map ($ a) fs
I have attempted, unsuccessfully, to write flist above in a point-free manner. Is it possible?
flist = flip (map . (flip ($)))
Note that if you define swing :: (((a -> b) -> b) -> c -> d) -> c -> a -> d swing f = flip (f . flip ($)) then you can get this sort of effect uniformly in a number of situations: swing map :: forall a b. [a -> b] -> a -> [b] swing any :: forall a. [a -> Bool] -> a -> Bool swing foldr :: forall a b. b -> a -> [a -> b -> b] -> b
On Wed, Oct 13, 2004 at 01:00:05PM -0400, Jacques Carette wrote:
-- |Apply list of functions to some value, returning list of results. -- It's kind of like an converse map. flist :: [a->b] -> a -> [b] flist fs a = map ($ a) fs
I have attempted, unsuccessfully, to write flist above in a point-free manner. Is it possible?
Jacques
Of course it is, but why? flist = flip (map . flip ($)) Groeten, Remi -- Nobody can be exactly like me. Even I have trouble doing it.
-- It's kind of like an converse map.
I have attempted, unsuccessfully, to write flist above in a point-free manner. Is it possible?
Of course it is, but why? flist = flip (map . flip ($))
Some functions are simpler point-free, others are simpler with points. I was curious about this one (I like the pointwise version better). Also, the statement "It's kind of like a converse map" becomes quite clear from the point-free way to write it, while still not so obvious in the pointwise version. Jacques
Johannes Waldmann <waldmann@imn.htwk-leipzig.de> writes:
I liked to think of it as just a syntactical convention (for years ...) but is it really at no cost? It does introduce extra function calls, that is, extra closures etc.? Can these be removed by ghc's optimizer?
It is inlined by GHC when optimization is turned on. In general it doesn't have to be optimized out - it's just a function - but it's easy to inline and eliminate, if the compiler performs any inlining at all. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
participants (7)
-
Cale Gibbard -
Graham Klyne -
Jacques Carette -
Johannes Waldmann -
Malcolm Wallace -
Marcin 'Qrczak' Kowalczyk -
Remi Turk