Questions tagged [function-composition]

Applying one function to the result of another is known as function composition: `(f.g)(x) = f(g(x))`

Applying one function to the result of another is known as function composition: (f.g)(x) = f(g(x)).

See also .

370 questions
747
votes
13 answers

What is the difference between . (dot) and $ (dollar sign)?

What is the difference between the dot (.) and the dollar sign ($)? As I understand it, they are both syntactic sugar for not needing to use parentheses.
Rabarberski
  • 21,632
  • 19
  • 69
  • 85
133
votes
7 answers

Haskell function composition (.) and function application ($) idioms: correct use

I have been reading Real World Haskell, and I am nearing the end, but a matter of style has been niggling at me to do with the (.) and ($) operators. When you write a function that is a composition of other functions you write it like: f = g .…
Robert Massaioli
  • 12,801
  • 6
  • 48
  • 71
90
votes
6 answers

Dot Operator in Haskell: need more explanation

I'm trying to understand what the dot operator is doing in this Haskell code: sumEuler = sum . (map euler) . mkList The entire source code is below. My understanding The dot operator is taking the two functions sum and the result of map euler and…
cbrulak
  • 14,071
  • 19
  • 56
  • 96
54
votes
3 answers

runST and function composition

Why does this typecheck: runST $ return $ True While the following does not: runST . return $ True GHCI complains: Couldn't match expected type `forall s. ST s c0' with actual type `m0 a0' Expected type: a0 -> forall s. ST s c0 …
Grzegorz Chrupała
  • 2,974
  • 14
  • 22
49
votes
6 answers

Haskell function composition operator of type (c→d) → (a→b→c) → (a→b→d)

Ordinary function composition is of the type (.) :: (b -> c) -> (a -> b) -> a -> c I figure this should generalize to types like: (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d A concrete example: calculating difference-squared. We could write…
jameshfisher
  • 26,641
  • 22
  • 94
  • 145
49
votes
2 answers

What does (f .) . g mean in Haskell?

I have seen a lot of functions being defined according to the pattern (f .) . g. For example: countWhere = (length .) . filter duplicate = (concat .) . replicate concatMap = (concat .) . map What does this mean?
47
votes
1 answer

function composition with reverse syntax

If I want to apply f first and g second, I have to write: g . f Is there another standard syntax that would allow me to write the functions in the reverse order? f g I know I can just invent my own syntax: compose f g x = g (f x) and…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
45
votes
5 answers

What is happening when I compose * with + in Haskell?

I'm trying to understand the result of (*) . (+) in Haskell. I know that the composition operator is just the standard composition of mathematical functions- so (f . g) = f (g x) But: (*) . (+) :: (Num (a -> a), Num a) => a -> (a -> a) -> a ->…
user2666425
  • 1,571
  • 1
  • 13
  • 20
38
votes
3 answers

Haskell Monad bind operator confusion

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental.…
Ord
  • 5,113
  • 4
  • 25
  • 42
33
votes
1 answer

Why is function composition in Haskell right associative?

Mathematically the function composition operation is associative. Hence: f . (g . h) = (f . g) . h Thus the function composition operation may be defined to be either left associative or right associative. Since normal function application in…
Aadit M Shah
  • 67,342
  • 26
  • 146
  • 271
32
votes
4 answers

How to multiply functions in python?

def sub3(n): return n - 3 def square(n): return n * n It's dead easy to compose functions in python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, when…
wim
  • 266,989
  • 79
  • 484
  • 630
32
votes
14 answers

Composing functions in python

I have an array of functions and I'm trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() …
31
votes
6 answers

Elegant way to combine multiple filtering functions in Haskell

Given the following filtering functions as unary predicates, f1 :: Int -> Bool f1 x = x > 30 f2 :: Int -> Bool f2 x = x < 60 f3 :: Int -> Bool f3 x = x `mod` 3 == 0 I'd like to filter a list of integers through all of them. Currently I'm doing…
Jivan
  • 16,401
  • 7
  • 56
  • 89
25
votes
6 answers

What does a fullstop or period or dot (.) mean in Haskell?

I really wish that Google was better at searching for syntax: decades :: (RealFrac a) => a -> a -> [a] -> Array Int Int decades a b = hist (0,9) . map decade where decade x = floor ((x - a) * s) …
Casebash
  • 100,511
  • 79
  • 236
  • 337
24
votes
1 answer

Understanding `andThen`

I encountered andThen, but did not properly understand it. To look at it further, I read the Function1.andThen docs def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm is a MultiMap instance. scala> mm res29:…
Kevin Meredith
  • 38,251
  • 58
  • 190
  • 340
1
2 3
24 25