On this page:
list
join
head
tail
range
map
filter
len
foldr
foldl
reverse
index
index*
inlst
left
right
mid
slice
append1
append
assoc
subst
subst*
heads
sort
zip
zipwith
flatten

3.6 Lists🔗ℹ

procedure

(list v ...)  list?

  v : any
Returns a list containing the given values.

procedure

(join a b)  pair?

  a : any
  b : any
Joins a and b into a pair. If b is Null, a list is created containing a. If b is a list, a is joined to the head of the list.

procedure

(head l)  any

  l : list?
Returns the head (first element) of the list l.

procedure

(tail l)  any

  l : list?
Returns the remainder of list l after the head. If the list has only one element, returns Null.

syntax

(range start to finish)

(range start to finish step n)
Generates a list of numbers, incrementing from start to finish by n. If no n is provided, defaults to 1. Note that, unlike BASIC’s for x = y to z, descending sequences where start is greater than finish can only be declared by including a negative n. Otherwise only Null will be returned.

procedure

(map fun l)  list?

  fun : fn?
  l : list?
Given a single-argument function fun, returns a list with fun applied to each item in l.

procedure

(filter fun l)  list?

  fun : fn?
  l : list?
Given a predicate fun, returns a new list containing only those elements of l for which fun returns True.

procedure

(len l)  number?

  l : list?
Returns the number of items in l.

procedure

(foldr fun base l)  any

  fun : fn?
  base : any
  l : list?
Given a function fun with two arguments, returns the cumulative result of fun being applied to consecutive pairs, starting from base and the rightmost element of l.

procedure

(foldl fun base l)  any

  fun : fn?
  base : any
  l : list?
Similar to foldr, except that it combines pairs from the left, starting with the head of l and base.

procedure

(reverse l)  list?

  l : list?
Returns a list with the order of l reversed.

procedure

(index n l)  any

  n : number?
  l : list?
Returns the nth entry of l, indexed from 1.

procedure

(index* l dims ...)  any

  l : list?
  dims : number?
Walks through nested lists according to the given dims, essentially finding index recursively for an arbitrary number of dimensions. For example, given a nested list three lists deep, (index* l 2 3 1) would return the 1st element of the third element of the 2nd lst, like so:

Examples:
> (def dave '(1 (2 3 (4 5)) 6))
> (index* dave 2 3 1)

4

Also, (l dims ...) can be used as a shorthand for index*:

Examples:
> (def dave '(1 (2 3 (4 5)) 6))
> (dave 2 3 1)

4

procedure

(inlst item l)  any

  item : any
  l : list?
Searches l for item, returning the index of item if found, or False if not.

procedure

(left l n)  list?

  l : list?
  n : number?
Returns a list of the leftmost n elements of l.

procedure

(right l n)  list?

  l : list?
  n : number?
Returns a list of the rightmost n elements of l.

procedure

(mid l idx n)  list?

  l : list?
  idx : number?
  n : number?
Returns n entries of l starting from index idx.

procedure

(slice l first [last])  list?

  l : list?
  first : number?
  last : number? = (len l)
Returns a slice of l, starting at first and ending at last. If last is not provided, it defaults to the end of the list.

procedure

(append1 l1 l2)  list?

  l1 : list?
  l2 : list?
Returns a list with l2 appended to the end of l1.

procedure

(append l ...)  list?

  l : list?
Returns a list of the given ls appended together in order.

procedure

(assoc tgt l)  list-or-false?

  tgt : any
  l : list?
Searches the heads of a list of lists l and returns the first matching list or False.

procedure

(subst tgt new l)  list-or-false?

  tgt : any
  new : any
  l : list?
Searches the heads of a list of lists l, and if it finds tgt, returns a new list with the tail of tgt substituted for a new list containing new. Otherwise, returns False.

procedure

(subst* tgt new l)  list-or-false?

  tgt : any
  new : any
  l : list?
Searches the heads of a list of lists l, and if it finds tgt, returns a new list with the tail of tgt substituted for new. Otherwise, returns False. For clarity’s sake, the following examples may be more illustrative of the difference between subst and subst*.

Examples:
> (def alst '((foo 1) (bar 2)))
> (subst 'foo 3 alst)

'((foo 3) (bar 2))

> (subst 'foo '(3) alst)

'((foo (3)) (bar 2))

> (subst* 'foo 3 alst)

'((foo . 3) (bar 2))

> (subst* 'foo '(3) alst)

'((foo 3) (bar 2))

procedure

(heads l)  list?

  l : list?
Returns a list of the heads of a list of lists.

procedure

(sort fun l)  list?

  fun : fn?
  l : list?
Sorts list l according to the comparison function fun.

procedure

(zip l1 l2)  list?

  l1 : list?
  l2 : list?
Returns a new list of lists combining l1 and l2. Excess length of either list is dropped.

procedure

(zipwith fun l1 l2)  list?

  fun : fn?
  l1 : list?
  l2 : list?
Returns a new list, combining the matching pairs of each list with fun. Excess length of either list is dropped.

procedure

(flatten lst)  list?

  lst : list?
Traverses a list, and flattens any nested lists into a single one-dimensional list.