The
Higher Order Function Package
The HOF package is part
of the Standard Extension. The purpose
of the HOF package is to provide many of the basic
higherorder utility functions used by programmers that
do not exist in the Shen kernel.
Functions
converge :
(A > A) > A > (A > A > boolean)
> A
Given f
, x and r; applies f to x n
times until the result of the nth application x_{n}
satsfies the relation r(x_{n1},x_{n}).
\\
repeatedly divide 7 by 2 until the result differs
from the previous by less than .01.
(converge (/. X (/ X 2)) 7 (/. X Y (< ( X Y)
.01)))
0.0068359375 : number
filter : (A
> boolean) > (list A) > (list A)
Given f
, x; keep all those elements of x that satisfy f.
\\
filter all even numbers
(filter (/. X (integer? (/ X 2))) [1 2 3 4 5 6 7 8
9])
[2 4 6 8] : (list number)
for : number
> number > (number > A) > (number
> number) > A
This function is driven by a macro and the signature is
given of the function that it compiles into. However the
macro determines the proper form of this function. The
syntax is
(for X = M
to N step Step Do)
or
(for X = M
to N Do)
The latter case is
equivalent to (for
X = M to N step (+ 1) Do). The value returned is the final form
\\ Print
numbers 1 to 10
(for X = 1 to 10 (output "~A~%" X))
1
2
3
4
5
6
7
8
9
10
"10
" : string
\\ Print
odd numbers 1 to 11.
(for X = 1 to 11 step (+ 2) (output "~A~%"
X))
1
3
5
7
9
11
"11
" : string
loop :
number > number > (number > A) >
(number > number) > (A > B > B)
> B > B
This
function is a generalisation of for but it accumulates the results. loop is driven by a macro and the
signature is given of the function that it compiles into.
However the macro determines the proper form of this
function. The syntax is
1. (loop
for X = M to N step Step Do using Constructor ending
with End)
or
2. (loop
for X = M to N Do using Constructor ending with End)
or
3. (loop
for X = M to N Do using Constructor
2. is short for (loop for X = M to N
step (+ 1) Do using Constructor ending with End).
3. is short for (loop for X = M to N
step (+ 1) Do using Constructor ending with 0).
\\ Sum
the numbers 1 to 10.
(loop for X = 1 to 10 X using (function +))
55
mapf : (A
> B) > (B > (list C) > (list C))
> (list A) > (list C)
Given f,
c, x; maps f over the elements
of x constructing the resulting list using c.
(mapf (+
1) (function cons) [1 2 3 4 5]) is the same as (map (+ 1) [1 2 3
4 5])
(mapf
(/. X X) (function append) [[1] [2] [3]]) gives [1 2 3]
reduce : (A
> B > A) > A > (list B) > A
Given f, x, y; applies the
constructor f recursively to the elements of y
until y is empty terminating with z.
\\ Sum
the numbers 19
(reduce (function +) 0 [1 2 3 4 5 6 7 8 9])
45
removeif :
(A > boolean) > (list A) > (list A)
Given f,
x; removes all elements in x that
satisfy f.
\\
Remove all even numbers from a list.
(removeif (/. X (integer? (/ X 2))) [1 2 3 4 5 6 7 8
9])
[1 3 5 7 9] : (list number)
sort : (A
> A > boolean) > (list A) > (list A)
Given f, x;
sort the elements in x according to f.
\\ sort
from high to low
(sort (function >) [23 51 67 89 21])
[89 67 51 23 21] : (list number)
