f . g or f g or f $ g?

Hello all I frequently get confused over f . g vs f g. I do understand the following With: g :: a->b f :: b ->c f.g :: a->c However f g is a type error, because then f would have to accept a function (a->b) as its first parameter, but it only accepts a b. Is there a way to easily remember when to use f.g and when to use f g without having to do this type algebra. -- Martin

Hi,
Hello all
I frequently get confused over f . g vs f g. I do understand the following
With:
g :: a->b f :: b ->c
f.g :: a->c
However
f g
is a type error, because then f would have to accept a function (a->b) as its first parameter, but it only accepts a b.
Is there a way to easily remember when to use f.g and when to use f g without having to do this type algebra.
Function composition is different than function application. Are you mixing these concepts? Emanuel

Hi, One more thing, you should know exactly when to use function composition or function application. You need firt to udenrstund what function application is. Emanuel

Hi, Dnia 2013-02-01, pią o godzinie 20:42 +0100, Martin Drautzburg pisze:
Hello all
I frequently get confused over f . g vs f g. I do understand the following
With:
g :: a->b f :: b ->c
f.g :: a->c
However
f g
is a type error, because then f would have to accept a function (a->b) as its first parameter, but it only accepts a b.
Is there a way to easily remember when to use f.g and when to use f g without having to do this type algebra.
Maybe this could be a hint, consider this: f . g == (.) f g Emanuel

Martin Drautzburg
I frequently get confused over f . g vs f g. I do understand the following
Out of the three choices "f . g", "f g" and "f $ g", only the dot operator has a special meaning. The others are the same thing. Remember the function composition operator from math? It's the circle operator. That's how you should read the dot. In fact, if you use Emacs, you can make it display the dot that way. The haskell-mode has this built in. (f . g) x = f (g x) There is also something interesting to note about application: f $ x = ($) f x ($) f x = f x From that follows: ($) f = f And from that follows: ($) = id Indeed: id :: a -> a ($) :: (a -> b) -> (a -> b) ($) is really just id with a more special type. I hope this helps. =) Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

Hi, Dnia 2013-02-12, wto o godzinie 22:09 +0100, Martin Drautzburg pisze:
On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote:
(f . g) x = f (g x)
so (f . g) x = f $ g x
right?
That looks like the two are pretty interchangeable. When would I prefer one over the other?
($) has lower precedence (it was introduced for that reason I belive). Prelude> :info ($) ($) :: (a -> b) -> a -> b -- Defined in GHC.Base infixr 0 $ Please take a look at: http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.h...
From the docs:
"Application operator. This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted..." Emanuel

Exactly. It is a convenience operator that lets you do: f x $ g y $ h z instead of f x (g y (h z)) As for whether you should write: foo = f . g or foo x = f $ g x ..go with whichever looks clearest and nicest to you. In most cases where you are just taking one argument and applying some functions to it, it's nice to omit the x. But in more complex cases, it may make your code harder to read and modify. See http://www.haskell.org/haskellwiki/Pointfree (and Problems with "pointless" style.) I personally always use pointfree where I would've required a lambda expression, e.g.: f (g . h) y instead of f (\x -> g $ h x) y On Tue, Feb 12, 2013 at 10:23 PM, Emanuel Koczwara < poczta@emanuelkoczwara.pl> wrote:
Hi,
Dnia 2013-02-12, wto o godzinie 22:09 +0100, Martin Drautzburg pisze:
On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote:
(f . g) x = f (g x)
so (f . g) x = f $ g x
right?
That looks like the two are pretty interchangeable. When would I prefer one over the other?
($) has lower precedence (it was introduced for that reason I belive).
Prelude> :info ($) ($) :: (a -> b) -> a -> b -- Defined in GHC.Base infixr 0 $
Please take a look at:
http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.h...
From the docs:
"Application operator. This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted..."
Emanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

You can write (f . g) x as f . g $ x so for me, it's avoiding extra
parenthesis.
Mukesh
On Wed, Feb 13, 2013 at 2:53 AM, Emanuel Koczwara wrote: Hi, Dnia 2013-02-12, wto o godzinie 22:09 +0100, Martin Drautzburg pisze: On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote: (f . g) x = f (g x) so (f . g) x = f $ g x right? That looks like the two are pretty interchangeable. When would I prefer
one
over the other? ($) has lower precedence (it was introduced for that reason I belive). Prelude> :info ($)
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $ Please take a look at: http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.h... From the docs: "Application operator. This operator is redundant, since ordinary
application (f x) means the same as (f $ x). However, $ has low,
right-associative binding precedence, so it sometimes allows parentheses
to be omitted..." Emanuel _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

An example that helped me understand the utility of the $ operator is as follows: suppose that we have a list of functions fs :: [a -> b] fs = [f1, f2, ..., fk] and a list of arguments as :: [a] as = [a1, a2, ..., ak] and we want to produce the list [f1(a1), f2(a2), ..., fk(ak)], or equivalently [f1 a1, f2 a2, ..., fk ak], this can be done using the $ operator via zipWith ($) fs as simply because it makes the function application explicit. ::paul On 2013-02-12, at 13:44 , mukesh tiwari wrote:
You can write (f . g) x as f . g $ x so for me, it's avoiding extra parenthesis.
Mukesh
On Wed, Feb 13, 2013 at 2:53 AM, Emanuel Koczwara
wrote: Hi, Dnia 2013-02-12, wto o godzinie 22:09 +0100, Martin Drautzburg pisze:
On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote:
(f . g) x = f (g x)
so (f . g) x = f $ g x
right?
That looks like the two are pretty interchangeable. When would I prefer one over the other?
($) has lower precedence (it was introduced for that reason I belive).
Prelude> :info ($) ($) :: (a -> b) -> a -> b -- Defined in GHC.Base infixr 0 $
Please take a look at: http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Prelude.h...
From the docs:
"Application operator. This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted..."
Emanuel
_______________________________________________ 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

mukesh tiwari
You can write (f . g) x as f . g $ x so for me, it's avoiding extra parenthesis.
Here comes my usual lecture: The ($) operator can be harmful when explicitly used to avoid extra parentheses, because it makes code flat. Besides grouping function application parentheses also serve a readability purpose, about the same one as indentation. In other words, think twice before you overuse ($). Background: In some of my projects I have noticed that to revisit old code I had an easier time rewriting some of my ($) applications back to parentheses to make the code structure more apparent and easier for the eye, particularly when both sides of ($) were long and multiple ($)s were involved. But even in very simple cases parentheses just look much nicer. Compare print $ sin x with print (sin x) The latter just looks nicer, at least to my eyes. Of course there are cases when ($) is really preferred, like when one side is very long, perhaps multi-lined, and the other is very short: log $ "Client connecting from " ++ hostname or: withSteak $ \steak -> do getHungry eatWith cutlery steak beHappy But then of course in many cases you get hungry long before you get the steak: getHungry withSteak (eatWith cutlery) beHappy Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

A less obvious interpretation is to treat ($) as `id'. (f . g) x = f $ g x = f (id g x) = f (g x) Bah, we can even illustrate that ($) is simply an infix identity function: Prelude> map ((flip id) 2) [\x -> x - 3] [-1] Prelude> map (`id` 2) [\x -> x - 3] [-1] Prelude> map ($ 2) [\x -> x - 3] [-1] I just wanted to throw this out there as I found this out recently myself and found it fairly useful. I believe someone else already posted a link about pointless style. On 12/02/13 21:09, Martin Drautzburg wrote:
On Friday, 1. February 2013 23:02:39 Ertugrul Söylemez wrote:
(f . g) x = f (g x)
so (f . g) x = f $ g x
right?
That looks like the two are pretty interchangeable. When would I prefer one over the other?

Mateusz Kowalczyk
A less obvious interpretation is to treat ($) as `id'.
(f . g) x = f $ g x = f (id g x) = f (g x)
This is not how you get from ($) to id. The correct path is: f $ x = ($) f x = f x = id f x = f `id` x This equivalence is indicated by the type of ($). It's a specialized instance of a -> a: ($) :: (a -> b) -> (a -> b) ($) f = f or equivalently: ($) :: (a -> b) -> a -> b ($) f x = f x or equivalently: ($) :: (a ~ b -> c) => a -> a ($) = id Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

Oops, you're right. Sorry for my oversight. On 13/02/13 08:12, Ertugrul Söylemez wrote:
Mateusz Kowalczyk
wrote: A less obvious interpretation is to treat ($) as `id'.
(f . g) x = f $ g x = f (id g x) = f (g x)
This is not how you get from ($) to id. The correct path is:
f $ x = ($) f x = f x = id f x = f `id` x
This equivalence is indicated by the type of ($). It's a specialized instance of a -> a:
($) :: (a -> b) -> (a -> b) ($) f = f
or equivalently:
($) :: (a -> b) -> a -> b ($) f x = f x
or equivalently:
($) :: (a ~ b -> c) => a -> a ($) = id
Greets, Ertugrul
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Feb 01, 2013 at 08:42:56PM +0100, Martin Drautzburg wrote:
Is there a way to easily remember when to use f.g and when to use f g without having to do this type algebra.
No. After enough practice, however, this sort of type algebra becomes second nature, and no longer requires much conscious effort. -Brent

On 1 February 2013 20:42, Martin Drautzburg
Hello all
I frequently get confused over f . g vs f g. I do understand the following
With:
g :: a->b f :: b ->c
f.g :: a->c
However
f g
is a type error, because then f would have to accept a function (a->b) as its first parameter, but it only accepts a b.
Is there a way to easily remember when to use f.g and when to use f g without having to do this type algebra.
If you're familiar with the analogous operations in mathematics (function composition and function application), it should be easy to reason about. Function application is the act of "calling" the function: passing it an argument and making it "return" a result. In math, we write function application with parentheses, i.e. cos(pi) which "returns" (or /has value of/) -1. The equivalent in Haskell would be written simply as cos pi Function composition on the other hand is the act of combining two functions (e.g. f1 and f2) so that the resultant function performs both operations in sequence. You can think of it as combining the functions in a pipeline so that the output of the first is passed as an argument to the second. Again, in mathematics, we'd write the composition of f1 and f2 as f2 ∘ f1 In Haskell, we would write the same as f2 . f1 This is a good mnemonic for remembering the role of (.) since the dot looks a bit like the small circle used for functional composition in mathematics. -- Denis Kasak
participants (9)
-
Brent Yorgey
-
Denis Kasak
-
Emanuel Koczwara
-
Ertugrul Söylemez
-
Martin Drautzburg
-
Mateusz Kowalczyk
-
mukesh tiwari
-
Patrick Mylund Nielsen
-
Paul Higham