Doesn't . and $ do the same thing? I always get confused about
that, like when would I use one over the other. |
The short answer is no, they're not the same.
Someone asked this exact question about a month ago, and there were some very good answer answers, but it's hard for me to find the thread. I can't find a search engine that'll let me search for keywords like "$" and ".". Anybody remember which thread this was? Tom / amindfv On Sun, Oct 30, 2011 at 1:39 PM, <haxl at nym.hush.com> wrote: > Doesn't . and $ do the same thing? I always get confused about > that, like when would I use one over the other. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111030/957d40c2/attachment.htm> |
In reply to this post by haxl@nym.hush.com
No, not really
the '.' operator is function composition, so f.g is a new function with f applied over g. h=f.g ==> h x = f ( g x ) the '$' is the apply operator, and it applies the right side to the left side: h = f $ x ==> h= f ( x ) You can check this looking at their types (.) :: (b -> c) -> (a -> b) -> a -> c ($) :: (a -> b) -> a -> b the first one takes two functions and return a new function. The second takes a function and a value and returns a value. Regards Rafael On Sun, Oct 30, 2011 at 15:39, <haxl at nym.hush.com> wrote: > Doesn't . and $ do the same thing? I always get confused about > that, like when would I use one over the other. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Rafael Gustavo da Cunha Pereira Pinto -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111030/bd4ad7f3/attachment.htm> |
In reply to this post by haxl@nym.hush.com
On Sun, Oct 30, 2011 at 6:39 PM, <haxl at nym.hush.com> wrote:
> Doesn't . and $ do the same thing? ?I always get confused about > that, like when would I use one over the other. > It is true that there is some overlap, mainly when you have a big stack of one parameter functions to apply : > result = f $ g $ h $ x or > result = f . g . h $ x The second form is preferred though for the good reason that it is easier to refactor : You can take any part of the composition chain, pull it out and give it a name (or use it somewhere else) without worry : > result = newFun . h $ x > where newFun = f . g Whereas with the first form you'll need to introduce a point (which you'll have to name) and sometimes rework your expression : > result = newFun $ h $ x > where newFun y = f $ g $ y Anyway as for the difference : f $ g $ h $ x == f (g (h x)) In other words you apply a function to the results of another function itself applied to another function applied to the point x. f . g . h $ x == (f . g . h) x You create a new function that is the composition of the three initial ones then apply it to the point x. Of course in practice the same operation occurs in both cases but conceptually you're more "functional" and "modular" in the second formulation. -- Jeda? |
In reply to this post by haxl@nym.hush.com
On Oct 30, 2011, at 1:39 PM, haxl at nym.hush.com wrote: > Doesn't . and $ do the same thing? I always get confused about > that, like when would I use one over the other. One good way to answer questions like this for yourself is to jump to the source. By looking at the type in ghci ":t (.)", I found that (.) if defined in GHC.Base. I downloaded the "base" git repo (http://darcs.haskell.org/packages/base.git/) because the online docs for GHC.Base are linked but the link is broken (http://hackage.haskell.org/packages/archive/base/4.4.0.0/doc/html/GHC-Base.html) and found the following: GHC/Prelude.lhs {-# INLINE (.) #-} -- Make sure it has TWO args only on the left, so that it inlines -- when applied to two functions, even if there is no final argument (.) :: (b -> c) -> (a -> b) -> a -> c (.) f g = \x -> f (g x) infixr 0 $ ... {-# INLINE ($) #-} ($) :: (a -> b) -> a -> b f $ x = f x So, we see that (.) returns a lambda of a simple nested function call while ($) is compiled away to perform no extra steps. They are similar in that Haskellers often use them to reduce parentheses but different in their outcomes. In particular, note the difference in types- (.) takes two function arguments while ($) takes only one. I am a beginner myself and I often find Haskell modules lightly documented (especially in comparison to more widely-used languages), so I find jumping to the source invaluable. Cheers, M |
Free forum by Nabble | Edit this page |