Glowing orbs in a starry nebula, joined by threads of light

Episode 9 of my video series on array programming is about functions as values. In many languages, a function is something you call: you give it arguments and get a result. In X_eTaL, a function is also a value. It can be passed to another function, held by a name, returned from a function, and given only part of what it needs.

Episode 8 ended with a preview: a quote in front of a function passes it. This episode is about what that makes possible.

Call it, or pass it

Write a function next to a value, and it applies. Put a quote in front, and you pass the function itself, here to e̲ach, which applies it to every item:

n̲eg 5                                              ⍝ -5
'n̲eg e̲ach 1 2 3                                    ⍝ -1 -2 -3

A name can hold a function

Like any value, a function can be bound to a name. Here ᵘf̲lip names the built-in reverse, and our own square is a function too, bound the same way:

ᵘf̲lip ← r̲ev
ᵘf̲lip 1 2 3                                        ⍝ 3 2 1
ᵘs̲quare ← { ⍵ × ⍵ }
ᵘs̲quare 1 2 3                                      ⍝ 1 4 9

A plain name, with no underline, can hold a function too. But a plain name is a value, not a function, so writing it next to a list is refused before the program runs:

f ← 'r̲ev
f 1 2 3
error[adjacent-values]: two values side by side
    (only literals of one kind form strands: numbers, or strings)

To apply it, wrap it in parentheses and add an underscore, typed (f)_. Drawn, the parentheses are underlined, like the first letter of a function’s name. Around reverse itself it works the same way, though that is just a longer way to write reverse:

f                                                  ⍝ <function>
(̲f)̲ 1 2 3                                          ⍝ 3 2 1
(̲r̲ev)̲ 1 2 3                                        ⍝ 3 2 1

And since a plain name is already a value, it is passed with no quote. A quote in front of it is refused:

g ← n̲eg
g e̲ach 1 2 3                                       ⍝ -1 -2 -3
error[bad-quote]: only functions can be quoted; a variable is already a value

One reduce, many functions

Reduce doesn’t need to know which operation you want: you pass it in. Plus gives the sum, times the product, max the largest and min the smallest. The same reduce, four functions. Scan is a reduce that keeps every step: with plus, the running sums; with max, the largest so far.

v ← 3 1 4 1 5
'+ r̲/ v                                            ⍝ 14
'× r̲/ v                                            ⍝ 60
'm̲ax r̲/ v                                          ⍝ 5
'm̲in r̲/ v                                          ⍝ 1
'+ s̲\ v                                            ⍝ 3 4 8 9 14
'm̲ax s̲\ v                                          ⍝ 3 3 4 4 5

Each, power, table and inner

e̲ach takes a function of your own, like square, or a lambda, an unnamed function in braces, with its quote. p̲ower applies a function a given number of times: square three times over turns 2 into 256. t̲able applies a function to every pair, so plus builds an addition table, and i̲nner takes two functions: plus and times together give the dot product.

'ᵘs̲quare e̲ach 1 2 3                                ⍝ 1 4 9
'{ ⍵ + 10 } e̲ach 1 2 3                             ⍝ 11 12 13
3 'ᵘs̲quare p̲ower 2                                 ⍝ 256
1 2 3 '+ t̲able 10 20                               ⍝ 11 21
                                                   ⍝ 12 22
                                                   ⍝ 13 23
1 2 3 '+ '× i̲nner 4 5 6                            ⍝ 32

Your own functions that take functions

Your own functions can take functions too. ᵘt̲wice takes a function and applies it two times; pass it double, and 5 becomes 20. Inside, the passed function is just a name, f̲, underlined because it is a function, and used like any other:

ᵘt̲wice ← { f̲ x → f̲ f̲ x }
ᵘd̲ouble ← { 2 × ⍵ }
'ᵘd̲ouble ᵘt̲wice 5                                  ⍝ 20

This one applies a function to two values and returns a pair. Pass it tally and two strings, and you get two counts; pass it reverse, and you get both reversed:

ᵘb̲oth ← { (f̲, a, b) → (f̲ a, f̲ b) }
ᵘb̲oth ('t̲ally, "hello", "hi")                      ⍝ (5, 2)
ᵘb̲oth ('r̲ev, "hello", "hi")                        ⍝ (olleh, ih)

But inside, the passed function has one type. So a string and a list of numbers together are refused:

ᵘb̲oth ('t̲ally, "hello", 1 2 3)
error[type-mismatch]: expected Char, found a number

A function that returns a function

A function can also return a function. ᵘa̲dder takes a number and gives back a lambda that adds it. ᵘa̲dd5 is made by it, and then used like any function:

ᵘa̲dder ← { n → { ⍵ + n } }
ᵘa̲dd5 ← ᵘa̲dder 5
ᵘa̲dd5 1 2 3                                        ⍝ 6 7 8

Every function takes one argument

In fact, every function in X_eTaL takes one argument. Two arguments are a shortcut: the function takes the left one and returns a function that takes the right one. That is called currying, and it makes giving only the left one useful. Two to the power is power, with 2 given:

ᵘp̲ow ← { b e → b ^ e }
2 ᵘp̲ow 10                                          ⍝ 1024
ᵘt̲woTo ← ᵘp̲ow 2
ᵘt̲woTo 10                                          ⍝ 1024

A symbol like minus needs both sides. Quote it, give it one side with the same parentheses and underscore, and it becomes a function: ten minus, applied to 3, is 7, and times two, passed to each, doubles every item. That is partial application:

ᵘt̲enMinus ← (̲'−)̲ 10
ᵘt̲enMinus 3                                        ⍝ 7
((̲'×)̲ 2) e̲ach 1 2 3                                ⍝ 2 4 6

A function with no argument still takes one: the unit value, written @. The name alone is the function, a value; applied to @, it runs:

ᵘa̲nswer ← { @ → 42 }
ᵘa̲nswer @                                          ⍝ 42
p̲i @                                               ⍝ 3.141592653589793

In short, and next time

  • A function is a value.
  • A quote passes it, and a name can hold it.
  • Functions can take functions, and return them: reduce, scan, each, power, table, inner, and your own.
  • Every function takes one argument: currying and partial application.

The next episode is about joining functions together: pipelines, composition and trains. This one ends with a preview: two functions composed into one, the same two as a train, and three as a fork, which gives the mean:

'n̲eg 'a̲bs c̲ompose -4                               ⍝ -4
[n̲eg a̲bs] -4                                       ⍝ -4
['+ r̲/ ÷ t̲ally] 1 2 3 4                            ⍝ 2.5

Before then, try the episode’s exercise in X_eTaL in your browser: write a function that takes a function, and pass it a built-in and one of your own.