star/test/corpus/higher_order.star

23 lines
491 B
Plaintext

// Test: Higher-order functions
let map = fn f list =>
match list with
| [] => []
| x :: xs => f(x) :: map(f, xs)
end
let filter = fn pred list =>
match list with
| [] => []
| x :: xs =>
if pred(x) then
x :: filter(pred, xs)
else
filter(pred, xs)
end
let main = fn () =>
let numbers = [1, 2, 3, 4, 5] in
let doubled = map(fn x => x * 2, numbers) in
let evens = filter(fn x => x % 2 == 0, numbers) in
print(doubled)