Functions
Because functions are values, there are also functions that operate on them:
($) :: (a -> <c> b) -> a -> <c> b (Prelude)
Function application. f $ x is equivalent with f x. The function has two uses.
First is to remove parentheses from deeply nested expressions:
f (g (h x)) == f $ g $ h x
The second use is with higher order functions:
map ($ parameter) functions
(.) :: (a -> <c> b) -> (d -> <c> a) -> d -> <c> b (Prelude)
Composes two functions
(f . g) x = f (g x)
Currying and partial application
Every SCL function is curried: a function that takes two arguments is actually a function
that takes one argument and returns another function expecting the second argument. The
types a -> b -> c and a -> (b -> c) are identical — -> is right-associative.
This means you can supply fewer arguments than the function expects. The result is a
partial application — a new function waiting for the remaining arguments:
add :: Integer -> Integer -> Integer
add x y = x + y
addFive :: Integer -> Integer
addFive = add 5 // partial application; addFive y = add 5 y
> map addFive [1,2,3]
[6, 7, 8]
Operators can also be partially applied by enclosing them in parentheses with one argument:
> map (+ 1) [1,2,3]
[2, 3, 4]
> filter (> 0) [-1, 2, -3, 4]
[2, 4]
The $ operator — low-precedence application
$ is the function application operator with the lowest precedence level, 1, which
it shares with & and the monadic operators such as >>= (right-associative). It lets
you eliminate a pair of parentheses by treating everything to its right as the argument:
f $ g x // same as f (g x)
print $ show $ 1 + 2 // print (show (1 + 2))
Without $:
print (show (1 + 2))
The & operator — reverse application
x & f is f x: the value comes first and the function after it. A chain of
transformations then reads in the order the value flows through them, from left to right:
> [1, 2, 3] & map (\x -> x * 2) & sum
12
Written with ordinary application, the same computation reads inside out:
sum (map (\x -> x * 2) [1, 2, 3]). The operands of & are evaluated in the order they
are written, the value first.
The sections of & are handy with higher-order functions: (2 &) applies a function to
2, and (& length) applies length to its argument:
> map (2 &) [\x -> x + 1, \x -> x * 10]
[3, 20]
> map (& length) [[1], [1, 2]]
[1, 2]
& is left-associative at precedence level 1, the same level as $ and the monadic
operators such as >>=. Unlike in Haskell, $ therefore does not bind more weakly than
&: everything to the left of a & is grouped first, so f $ x & g means
(f $ x) & g, and the compiler does not warn about the mix:
> show $ [1, 2] & length // (show $ [1, 2]) & length: the length of "[1, 2]"
6
> show ([1, 2] & length)
"2"
Write the parentheses whenever & meets $, >>=, >>, := or >=> in the same
expression.
The . operator — function composition
. composes two functions: (f . g) x = f (g x). The right-hand function is applied
first, its result is passed to the left-hand function.
> (neg . abs) (-5)
-5
> map (show . (* 2)) [1,2,3]
["2", "4", "6"]
Important: write a space after the .. show . sum and show. sum are both
composition, but without the space the dot attaches to the following name:
show . sum // composition
show.sum // ERROR: read as the name `sum` in a module imported as `show`
show .sum // ERROR: read as child access on a Simantics variable
In a module with the fields feature, the attached form is record field access instead
(see 1.13 Data types).
Backtick infix syntax
Any two-argument function can be used as an infix operator by surrounding it with
backticks:
10 `div` 3 // same as div 10 3
"hello" `isPrefixOf` "hello world"
The flip function
flip swaps the first two arguments of a function. It is useful when the natural argument
order does not match what a higher-order combinator expects:
flip :: (a -> b -> <d> c) -> b -> a -> <d> c (Prelude)
Flips the parameters of a binary function.
> flip div 2 10 // div 10 2 = 5
5
> map (flip splitString ";") lines // splitString line ";"
|