On this page:
list-prefix?
take-common-prefix
drop-common-prefix
split-common-prefix
filter-multiple
extend
check-duplicate
map/  values
map2
remf
group-by
cartesian-product
list-update
list-set

14 Lists

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

 (require unstable/list) package: unstable-list-lib

procedure

(list-prefix? l r)  boolean?

  l : list?
  r : list?
True if l is a prefix of r.

Example:

> (list-prefix? '(1 2) '(1 2 3 4 5))

#t

procedure

(take-common-prefix l r #:same? same?)  list?

  l : list?
  r : list?
  same? : equal?
Returns the longest common prefix of l and r.

Example:

> (take-common-prefix '(a b c d) '(a b x y z))

'(a b)

procedure

(drop-common-prefix l r #:same same?)  
list? list?
  l : list?
  r : list?
  same? : equal?
Returns the tails of l and r with the common prefix removed.

Example:

> (drop-common-prefix '(a b c d) '(a b x y z))

'(c d)

'(x y z)

procedure

(split-common-prefix l r #:same? same?)  
list? list? list?
  l : list?
  r : list?
  same? : equal?
Returns the longest common prefix together with the tails of l and r with the common prefix removed.

Example:

> (split-common-prefix '(a b c d) '(a b x y z))

'(a b)

'(c d)

'(x y z)

The subsequent bindings were added by Sam Tobin-Hochstadt.

procedure

(filter-multiple l f ...)  
list? ...
  l : list?
  f : procedure?
Produces (values (filter f l) ...).

Example:

> (filter-multiple (list 1 2 3 4 5) even? odd?)

'(2 4)

'(1 3 5)

procedure

(extend l1 l2 v)  list?

  l1 : list?
  l2 : list?
  v : any/c
Extends l2 to be as long as l1 by adding (- (length l1) (length l2)) copies of v to the end of l2.

Example:

> (extend '(1 2 3) '(a) 'b)

'(a b b)

The subsequent bindings were added by Ryan Culpepper.

procedure

(check-duplicate lst    
  [#:key extract-key    
  #:same? same?])  (or/c any/c #f)
  lst : list?
  extract-key : (-> any/c any/c) = (lambda (x) x)
  same? : 
(or/c (any/c any/c . -> . any/c)
      dict?)
 = equal?
Returns the first duplicate item in lst. More precisely, it returns the first x such that there was a previous y where (same? (extract-key x) (extract-key y)).

The same? argument can either be an equivalence predicate such as equal? or eqv? or a dictionary. In the latter case, the elements of the list are mapped to #t in the dictionary until an element is discovered that is already mapped to a true value. The procedures equal?, eqv?, and eq? automatically use a dictionary for speed.

Examples:

> (check-duplicate '(1 2 3 4))

#f

> (check-duplicate '(1 2 3 2 1))

2

> (check-duplicate '((a 1) (b 2) (a 3)) #:key car)

'(a 3)

> (define id-t (make-free-id-table))
> (check-duplicate (syntax->list #'(a b c d a b))
                   #:same? id-t)

#<syntax:13:0 a>

> (dict-map id-t list)

'((#<syntax:13:0 a> #t)

  (#<syntax:13:0 d> #t)

  (#<syntax:13:0 b> #t)

  (#<syntax:13:0 c> #t))

The subsequent bindings were added by Carl Eastlund.

procedure

(map/values n f lst ...)  
(listof B_1) ... (listof B_n)
  n : natural-number/c
  f : (-> A ... (values B_1 ... B_n))
  lst : (listof A)
Produces lists of the respective values of f applied to the elements in lst ... sequentially.

Example:

> (map/values
   3
   (lambda (x)
     (values (+ x 1) x (- x 1)))
   (list 1 2 3))

'(2 3 4)

'(1 2 3)

'(0 1 2)

procedure

(map2 f lst ...)  
(listof B) (listof C)
  f : (-> A ... (values B C))
  lst : (listof A)
Produces a pair of lists of the respective values of f applied to the elements in lst ... sequentially.

Example:

> (map2 (lambda (x) (values (+ x 1) (- x 1))) (list 1 2 3))

'(2 3 4)

'(0 1 2)

The subsequent bindings were added by David Van Horn.

procedure

(remf pred lst)  list?

  pred : procedure?
  lst : list?
Returns a list that is like lst, omitting the first element of lst for which pred produces a true value.

Example:

> (remf negative? '(1 -2 3 4 -5))

'(1 3 4 -5)

The subsequent bindings were added by Vincent St-Amour.

procedure

(group-by extract-key lst [=?])  (listof (listof A))

  extract-key : (-> A B)
  lst : (listof A)
  =? : (-> B B any/c) = equal?
Groups the given list into equivalence classes, with equivalence being determined by =?.

Example:

> (group-by (lambda (x) (modulo x 3)) '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))

'((0 54) (2 2 5 2 2 2) (1 643 7 43 1 1))

procedure

(cartesian-product lst ...)  (listof (listof A))

  lst : (listof A)
Computes the n-ary cartesian product of the given lists.

Examples:

> (cartesian-product '(1 2 3) '(a b c))

'((1 a) (1 b) (1 c) (2 a) (2 b) (2 c) (3 a) (3 b) (3 c))

> (cartesian-product '(4 5 6) '(d e f) '(#t #f))

'((4 d #t)

  (4 d #f)

  (4 e #t)

  (4 e #f)

  (4 f #t)

  (4 f #f)

  (5 d #t)

  (5 d #f)

  (5 e #t)

  (5 e #f)

  (5 f #t)

  (5 f #f)

  (6 d #t)

  (6 d #f)

  (6 e #t)

  (6 e #f)

  (6 f #t)

  (6 f #f))

The subsequent bindings were added by Eric Dobson.

procedure

(list-update lst index updater)  list?

  lst : list?
  index : (and/c (>=/c 0) (</c (length lst)))
  updater : (-> any/c any/c)
Returns a list that is the same as lst except at the specified index. The element at the specified index is (updater (list-ref lst index)).

Example:

> (list-update '(zero one two) 1 symbol->string)

'(zero "one" two)

procedure

(list-set lst index value)  list?

  lst : list?
  index : (and/c (>=/c 0) (</c (length lst)))
  value : any/c
Returns a list that is the same as lst except at the specified index. The element at the specified index is value.

Example:

> (list-set '(zero one two) 2 "two")

'(zero one "two")