What can I do with lists and functions?
The @
operator is used to apply a function to every element in a container:
SequenceList(1, 10) @ (n ↦ prime(n))
2 3 5 7 11 13 17 19 23 29
Functions can also be used to filter a container:
filter(ans, (n ↦ even(sum(digits(n)))))
2 11 13 17 19
The list is not the only kind of “container”; for instance, matrices and vectors are also containers:
A ≔ RandomIntMatrix(5, 0, 10)
⎛3 4 2 7 8⎞ ⎜7 7 2 3 4⎟ ⎜6 2 1 6 7⎟ ⎜2 6 9 4 2⎟ ⎝0 7 0 4 9⎠
ans @ sqr
⎛ 9 16 4 49 64⎞ ⎜49 49 4 9 16⎟ ⎜36 4 1 36 49⎟ ⎜ 4 36 81 16 4⎟ ⎝ 0 49 0 16 81⎠
Using Algosim, you can make most things “computable”:
Alice ≔ ExampleData("Alice in Wonderland"); BarChart( SortBy( frequencies( filter( characters(UpperCase(Alice)), ChrIsLetter) ), (x ↦ −x[2]) ) )