2

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?

duplode
  • 33,731
  • 7
  • 79
  • 150
brander
  • 121
  • 4
  • 2
    You might want to know there is a haskell wiki [page](https://wiki.haskell.org/Pointfree) on this. – wizzup Jan 23 '17 at 05:35

1 Answers1

4

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555