pair
List of symbols:
car, cdr, cons, list, list-append, xar!, xdr!
car
Usage: (car pair)
Namespace: root
Return the car (first item) from a pair. If used on a proper list this will be the first element.
Example:
(def tst-pairs-two (list 'x 'y 'z))
(test::assert-equal 'x (car tst-pairs-two))
(test::assert-equal 10 (car '(10)))
(test::assert-equal 9 (car '(9 11 13)))
cdr
Usage: (cdr pair)
Namespace: root
Return the cdr (second item) from a pair. If used on a proper list this will be the list minus the first element.
Example:
(def tst-pairs-three (list 'x 'y 'z))
(test::assert-equal '(y z) (cdr tst-pairs-three))
(test::assert-equal nil (cdr '(10)))
(test::assert-equal '(13) (cdr '(9 13)))
(test::assert-equal '(11 13) (cdr '(9 11 13)))
cons
Usage: (cons item collection)
Namespace: root
Prepends item to collection, forms a pair.
Example:
(test::assert-equal '(1 2 3) (cons 1 (list 2 3)))
(test::assert-equal (cons 1 (cons 2 (cons 3 '()))) '(1 2 3))
(test::assert-equal (cons 1 2) (1 . 2))
(test::assert-equal (cons 1 nil) (1))
(test::assert-equal (cons nil 2) (nil . 3))
(test::assert-equal (cons nil nil) (nil))
(test::assert-equal (cons 1 (cons 2 (cons 3 (cons 4 nil)))) '(1 2 3 4))
(test::assert-equal (cons 1 2) (1 . 2))
(test::assert-equal (cons 1 '(2 3 4)) (1 2 3 4))
list
Usage: (list item0 item1 .. itemN)
Namespace: root
Create a proper list from pairs with items 0 - N.
Example:
(test::assert-equal '(1 2 3) (list 1 2 3))
(test::assert-equal '() (list))
list-append
Usage: (list-append list item)
Namespace: root
If last parameter is a list it will be appened to the first list, otherwise adds item as pair.
Example:
(test::assert-equal (list-append (list 1 2 3) (list 1)) (list 1 2 3 1))
;; demonstrates that appending two lists is different than appending a non-list value
(test::assert-not-equal (list-append (list 1 2 3) (list 1)) (list-append (list 1 2 3) 1))
(test::assert-equal (list 1 2 3 4 5 6 7 8 9) (list-append (list 1 2 3) (list 4 5 6) (list 7 8 9)))
(test::assert-equal (list-append '(:a :b :c) '(:d :e :f) '() '(:g)) '(:a :b :c :d :e :f :g))
;; TODO PC unrepresentable?
(test::assert-equal (list-append '(1 2 3) 4) '(1 2 3 . 4))
(def lst '(:a :b :c))
(test::assert-equal (list-append lst (list :d)) (list :a :b :c :d))
(test::assert-equal lst '(:a :b :c))
(test::assert-equal (list-append) nil)
(test::assert-equal (list-append :a) :a)
xar!
Usage: (xar! pair expression)
Namespace: root
Destructive form that replaces the car (first item) in a pair with a new expression.
If used on a proper list will replace the first item. Can be used on nil to create a pair (expression . nil).
Example:
(def tst-pairs-three (list 'x 'y 'z))
(test::assert-equal '(x y z) tst-pairs-three)
(test::assert-equal '(s y z) (xar! tst-pairs-three 's))
(test::assert-equal '(s y z) tst-pairs-three)
(def tst-pairs-four (list 't))
(test::assert-equal '(y) (xar! tst-pairs-four 'y))
(test::assert-equal '(y) tst-pairs-four)
xdr!
Usage: (xdr! pair expression)
Namespace: root
Destructive form that replaces the cdr (second item) in a pair with a new expression.
If used on a proper list will replace everything after the first item. Can be used on nil to create a pair (nil . expression).
Example:
(def tst-pairs-five (list 'a 'b 'c))
(test::assert-equal '(a b c) tst-pairs-five)
(test::assert-equal '(a y z) (xdr! tst-pairs-five '(y z)))
(test::assert-equal '(a y z) tst-pairs-five)
(def tst-pairs-six (list 'v))
(test::assert-equal (list 'v) tst-pairs-six)
(test::assert-equal '(v . v) (xdr! tst-pairs-six 'v))
(test::assert-equal '(v . v) tst-pairs-six)