[Haskell] is $ a no-op?
Graham Klyne
GK at ninebynine.org
Wed Oct 13 11:54:38 EDT 2004
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
More information about the Haskell
mailing list