-
Notifications
You must be signed in to change notification settings - Fork 4
pair
The humble pair is the simplest compound data structure in Sexy. It consists of two values - a head and a tail. A list is a pair whose tail is either another pair or the empty list (). The empty list () is self-evaluating in Sexy.
(pair 2 3) -> (pair: 2 3)
(pair 2 (pair 3 (pair 5 ()))) -> (2 3 5)
(list 2 3 5) -> (2 3 5)
(quote (2 3 5) -> (2 3 5)
'(2 3 5) -> (2 3 5)
() -> ()
Pairs and lists are fundamental to all Lisp dialects; the code itself is composed of them.
(pair? x) -> returns true if x is a pair, or a list with size > 0.
(list? x( -> returns true if x is a proper list (a series of pairs ending in ()) or the empty list ().
(def p (pair 2 3))
(def xs (list 3 4 5))
(pair? p) -> true
(list? p) -> false
(pair? xs) -> true
(list? xs) -> true
(pair? ()) -> false
(list ()) -> true
x.n, (x n) -> if n is a number, return the (n-1)th value of the list.
(def xs (list 1 2 3))
xs.0 -> 1
(xs 0) -> 1
xs.2 -> 3
x.type -> pair ( empty if x is () )
x.empty? -> true if x is (), false otherwise
x.to-bool -> false if x is (), true otherwise
x.head, x.key -> first item in pair
x.tail, x.val -> second item in pair
(def p (pair 2 3))
(def xs (list 1 2 3))
p.head -> 2
p.key -> 2
p.tail -> 3
p.val -> 3
xs.head -> 1
xs.tail -> (2 3)
x.size -> number of items in the list
x.clone -> copy the list
x.reverse -> reverse the list
(x.cons item) -> return a new list with item added to the front. Same as (pair item x).
(x.has? item) -> true if item is a member of the list, false otherwise
(x.append xs) -> combine two lists
(x.take n), (x.drop n) -> return a new list with elements dropped
(def xs (list 1 2 3))
(xs.take 2) -> (1 2)
(xs.drop 2) -> (3)
(x.apply args) -> this allows lists to be used at the head of a code list.
(def xs (list 1 2 3))
(xs 'size) -> (send xs 'size) -> 3
x.to-list -> x
x.to-vector -> return a vector with identical elements
(send (list 1 2 3) 'to-vector) -> (vector: 1 2 3)
x.to-text -> return a text. Works only on lists composed solely of runes.
(def my-runes (list $h $e $l $l $o))
my-runes.to-text => "hello"
x.to-record -> return a record. Works only on lists composed solely of pairs (association lists).
(def alist (list (% x 1) (% y 2)))
alist.to-record -> (record: x 1 y 2)
(x.fold init proc) -> apply the procedure proc to successive items of the list, returning a single value
(x.reduce init proc) -> like fold, but breadth-first
(x.map proc) -> execute proc for each item of the list and return a list of results
(x.each proc) -> execute proc for each item of the list, and return nothing. Used for side effects.
(x.filter predicate) -> return a list of items for whom the predicate returned true
(x.sort predicate) -> sort the list according to the given predicate
(def nums (list 8 6 7 5 3 0 9))
(nums.fold 0 +) -> 38
(nums.reduce 0 +) -> 38
(nums.map (proc (x) (* 10 x))) -> (80 60 70 50 30 0 90)
(nums.filter (proc (x) (> x 5))) -> (8 6 7 9)
(nums.sort <) -> (0 3 5 6 7 8 9)
(nums.sort >) -> (9 8 7 6 5 3 0)
(xs.each (proc (x) (sys.say x))
8
6
7
5
3
0
9
-> null