sref seq value index
Sets indexed entry in a list, string, or hash table to
the given value.

>(let x (copy "abc") ; make the string literal mutable
(sref x #\d 1) x)
"adc"
>(do
(= x '(1 2 3))
(sref x 4 1) x)
(1 4 3)

count test seq
Counts the number of elements of seq that satisfy test . test is an object or predicate. For a table, the elements are the values.

>(count #\a "banana")
3
>(count (fn (_) (odd _)) '(1 2 3 4 5))
3

union f xs ys
Takes union of sequence xs and ys . Predicate f is used to determine equality to filter out duplicates. xs and ys must both be lists or strings.

>(union is '(1 2 3) '(2 3 4))
(1 2 3 4)
>(union is "ab" "banana")
"abnn"
>(union (fn (a b) (is (mod a 10) (mod b 10))) '(1 2 3) '(13 24 35))
(1 2 3 24 35)

len seq
Computes the length of seq .

>(len "abc")
3
>(len '(1 2 3))
3
>(len (obj a 1 b 2))
2

len< seq n
Tests if length of seq is less than n .

>(len< "abc" 4)
t
>(len< '(1 2 3) 4)
t
>(len< (obj a 1 b 2) 4)
t

len> seq n
Tests if length of seq is greater than n .

>(len> "abc" 4)
nil
>(len> '(1 2 3) 4)
nil
>(len> (obj a 1 b 2) 4)
nil

dedup seq
Returns contents of seq without duplicates. For a string, returns a list of characters. For a table, returns a list of values.

>(dedup '(1 2 3 2 1))
(1 2 3)
>(dedup "abcba")
(#\a #\b #\c)
>(dedup (obj a 1 b 2 c 1))
((a 1) (c 1) (b 2))

single list
Returns true if given a list of length one.

>(single '(1))
t
>(single 1)
nil
>(single '())
nil

pos test seq [start]
Returns the index of the first element of seq that satisfies test . seq is a list or string. test is either an object or predicate function. If start is given, testing starts at that element.

>(pos 'c '(a b c d))
2
>(pos #\c "abcd")
2
>(pos #\c "abcdc" 3)
4
>(pos odd '(2 4 5 6 7))
2
>(pos odd '(2 4 6))
nil

before t1 t2 seq [start]
Tests if t1 is true before t2 in seq . seq is either a list or string. The tests are either objects or predicate functions. If start is given, search starts with the specified element.

>(before 4 odd '(2 4 1 3))
t
>(before 4 odd '(2 3 4 1))
nil
>(before #\a #\n "banana")
t
>(before #\a #\n "banana" 2)
nil

randelt seq
Returns a random element from a list, or a random character from a string. It also works on a table with integer keys from 0 to n. Renamed from randomelt in arc3.

>(randelt '(1 2 3))
1
>(randelt "abcd")
#\b

mismatch s1 s2
Compares sequences and returns the position of the
first mismatch (as determined by is ). Returns nil if
the sequences are identical.

>(mismatch "abcde" "abXde")
2
>(mismatch '(1 2 3) '(1 9 3))
1
>(mismatch "abc" "abc")
nil

find test seq
Finds and returns the first element of seq that satisfies test (object or predicate). seq can be a list or string.

>(find odd '(2 3 4 5))
3
>(find odd '(2 4 6))
nil
>(find alphadig "...abc...")
#\a

cut seq start [end]
Returns subsequence of seq from start to end . If end is not specified, the remainder of seq is used. The seq can be a list or string.

>(cut "abcde" 2)
"cde"
>(cut "abcde" 2 4)
"cd"
>(cut '(a b c d) 2)
(c d)
>(cut '(a b c d) 2 4)
(c d)

rem test seq
Removes elements from seq that satisfy test . test is either a function or an object. seq is either a list or string.

>(rem odd '(1 2 3 4 5))
(2 4)
>(rem 3 '(1 2 3 4 5))
(1 2 4 5)
>(rem #\c "abcde")
"abde"
>(rem (fn (_) (in _ #\a #\b)) "abcde")
"cde"

keep test seq
Keeps elements from seq that satisfy test . test is either a function or an object. seq is either a list or string.

>(keep odd '(1 2 3 4 5))
(1 3 5)
>(keep 3 '(1 2 3 4 5))
(3)
>(keep #\c "abcde")
"c"
>(keep (fn (_) (in _ #\a #\b)) "abcde")
"ab"

map f [seq ...]
Applies f to the elements of the sequences, taking the first from each, the second from each, and so on. If there are n sequences, f must be a function accepting n arguments. The sequences can be lists or strings. If any sequence is a string, then f must return a character, and the result will be a string made up of the results from f . Otherwise, the result will be a list of the results from f . The sequences are processed up to the length of the shortest sequence. For a single list, map is the same as map1 .

>(map (fn (a b c) (+ (* a 100) (* b 10) c))
'(1 2 3) '(4 5 6) '(7 8 9 10))
(147 258 369)
>(map (fn (_) (list _ (* _ 10))) '(1 2 3))
((1 10) (2 20) (3 30))
>(map cdr '((1) (2 3) (4 5)))
(nil (3) (5))
>(map (fn (c n) (coerce (+ n (coerce c 'int)) 'char)) "abc" '(0 2 4))
"adg"
>(map min "bird" "elephant")
"bied"

sum f seq
Applies f to the elements of the sequence and sums the results. New in arc3.

>(sum int "abc")
294
>(sum log '(1 2 3))
1.791759469228055
>(sum cadr (obj a 1 b 2 c 3))
6

get index
Generates a function to get the element referenced by index; the function can be applied to a table. This is useful for mapping, for instance. (It can also be applied to functions, not jus sequences.) New in arc3.

>(map get.2 '((a b c) (1 2 3) (p q r)))
(c 3 r)
>(get!b (obj a 10 b 20))
20
>(get.42 log)
3.7376696182833684
