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
list-update
list-set

17 Lists

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

 (require unstable/list)

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 c> #t)

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

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

  (#<syntax:13:0 d> #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 =? lst [#:key extract-key])  (listof (listof A))

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

Example:

> (group-by = '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))

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

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")