Reputation: 121
I am trying to use partial application to shorten the following valid function definition:
ltest50 n = take 50 (iterate ltestonce n)
I thought something like:
ltest50 = take 50 (iterate ltestonce)
or
ltest50 = take 50 (iterate ltestonce$)
would do the trick, but no joy. What is the best way to do this in Haskell?
Upvotes: 2
Views: 324
Reputation: 477749
Haskell has the "dot operator" or "function composition" (.) :: (b -> c) -> (a -> b) -> a -> c
for this:
ltest50 n = take 50 (iterate ltestonce n)
is equivalent to:
ltest50 = take 50 . iterate ltestonce
(.)
is defined as:
(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g x = f (g x)
or a bit shorter:
(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g x = f $ g x
Since it is a symbol placed between brackets, that means it can be used as .
as an infix operator, so:
ltest50 = take 50 . iterate ltestonce
is "syntactical sugar" for:
ltest50 = (.) (take 50) (iterate ltestonce)
At least in Flanders (the Dutch part of Belgium) the operator is sometimes informally called "after" because you apply f
after you have applied g
on a variable.
Upvotes: 4