Skip to content
Jan Špaček edited this page Apr 12, 2016 · 1 revision

A cons pair is an immutable pair of values, traditionally called car and cdr. Even though there is no restriction on the values stored in a cons, it is intended to be used to make linked lists. The empty list is commonly represented with false.

  • Cons cells

    • (cons a d) makes a new cons with car of a and cdr of d.
    • (cons? c) returns true if c is a cons cell.
    • (car c) and (cdr c) return the corresponding component.
    • (caar c), (cadr c), (cdar c) and (cddr c) are composed cars and cdrs.
  • Lists

    • null is an alias for false, representing an empty list.
    • (null? x) is an alias for not, more readable when used with lists.
    • (list? xs) returns true if xs is null or a cons pair where cdr is a list.
    • (list-len xs) returns the length of linked list xs.
    • (list-tail xs k) drops the first k elements from the list xs and returns the rest.
    • (list-reverse xs) reverses the list xs.
  • Lists as sets and maps

    • (member-eqv x xs) tries to find an element in xs that is eqv? to x. It returns the first sublist containing that value or null if x is not present in the list.
    • (member-equal x xs) does the same as member-eqv, but uses equal? for comparison.
    • (assoc-eqv key pairs) looks up key in pairs, which should be a list of cons key-value cells. It returns the matching value or null if it is not found.
    • (assoc-equal key pairs) works as assoc-eqv, but compares with equal?.