-- List operations: hd, tl, slc, rev, srt, flt, map, fld.
-- hd = first element, tl = all but first, slc = slice, rev = reverse.
-- flt = filter, map = transform, fld = fold (reduce).
-- First element of a list
first xs:L n>n;hd xs
-- All but first
rest xs:L n>L n;tl xs
-- Slice list from index 1 to 3 (exclusive end)
mid xs:L n>L n;slc xs 1 3
-- Reverse a list
flip xs:L n>L n;rev xs
-- Sort a list of numbers
sorted xs:L n>L n;srt xs
-- vm/jit lack higher-order builtins (flt, map, fld passing function refs); jit also can't bind list args
-- engine-skip: vm jit
-- run: first [10,20,30]
-- out: 10
-- run: rest [10,20,30]
-- out: [20, 30]
-- run: mid [10,20,30,40,50]
-- out: [20, 30]
-- run: flip [1,2,3,4,5]
-- out: [5, 4, 3, 2, 1]
-- run: sorted [3,1,4,1,5,9,2,6]
-- out: [1, 1, 2, 3, 4, 5, 6, 9]
-- Filter: keep only positive numbers
pos x:n>b;>x 0
keep-pos xs:L n>L n;flt pos xs
-- Map: double every element
dbl x:n>n;*x 2
dbl-all xs:L n>L n;map dbl xs
-- Fold (reduce): sum via fold, initial accumulator 0
add x:n y:n>n;+x y
fold-sum xs:L n>n;fld add xs 0
-- run: keep-pos [-1,2,-3,4,-5]
-- out: [2, 4]
-- run: dbl-all [1,2,3]
-- out: [2, 4, 6]
-- run: fold-sum [1,2,3,4,5]
-- out: 15