On this page:
make-alist
build-alist
map-map
member?
memv?
memq?
partition*
take-while
take-until
drop-while
drop-until
sort/  unique

6.1 mischief/list: Lists and Pairs

 (require mischief/list) package: mischief

procedure

(make-alist keys value)  (listof cons?)

  keys : list?
  value : any/c
Returns an association list in which the cars of the elements are the given keys in order and the cdr of each element is value.

Example:
> (make-alist '(1 2 3) 'number)

'((1 . number) (2 . number) (3 . number))

procedure

(build-alist keys proc)  (listof cons?)

  keys : list?
  proc : (-> any/c any/c)
Returns an association list in which the cars of the elements are the given keys in order and the cdr of each element is the result of proc applied to its car.

Example:
> (build-alist '(one two three) symbol->string)

'((one . "one") (two . "two") (three . "three"))

procedure

(map-map proc xs ...+)  (listof list?)

  proc : (-> any/c ...+ any/c)
  xs : (listof list?)
Maps proc over each element in one or more lists of lists.

Examples:
> (map-map string->symbol '(("bat" "cat") ("hat" "sat") ("mat")))

'((bat cat) (hat sat) (mat))

> (map-map + '((1 2) (3)) '((4 5) (6)))

'((5 7) (9))

procedure

(member? x xs [equiv?])  boolean?

  x : any/c
  xs : list?
  equiv? : (-> any/c any/c boolean?) = equal?

procedure

(memv? x xs)  boolean?

  x : any/c
  xs : list?

procedure

(memq? x xs)  boolean?

  x : any/c
  xs : list?
Variants of member, memv, and memq that return #true instead of a suffix of xs.

Examples:
> (member? "cat" '("bat" "cat" "hat"))

#t

> (member? "mat" '("bat" "cat" "hat"))

#f

> (memv? 3 '(1 2 3))

#t

> (memv? 12 '(1 2 3))

#f

> (memq? 'a '(a b c))

#t

> (memq? 'd '(a b c))

#f

procedure

(partition* pred xs ...)  
list? ... list? ...
  pred : (-> any/c ... boolean?)
  xs : list?
Partitions multiple lists based on pred as a generalization of partition. Returns results as multiple values: the lists of respective values for which pred was true followed by the lists of respective values for which pred was false.

Example:
> (partition* = '[1 2 3 4] '[1.0 -2.0 3.0 -4.0])

'(1 3)

'(1.0 3.0)

'(2 4)

'(-2.0 -4.0)

procedure

(take-while pred xs)  list?

  pred : predicate/c
  xs : list?

procedure

(take-until pred xs)  list?

  pred : predicate/c
  xs : list?

procedure

(drop-while pred xs)  list?

  pred : predicate/c
  xs : list?

procedure

(drop-until pred xs)  list?

  pred : predicate/c
  xs : list?
Produce a prefix or suffix of xs based on applying pred to elements at the front of xs.

Examples:
> (take-while negative? '[-2 -1 0 1 2])

'(-2 -1)

> (take-until positive? '[-2 -1 0 1 2])

'(-2 -1 0)

> (drop-while negative? '[-2 -1 0 1 2])

'(0 1 2)

> (drop-until positive? '[-2 -1 0 1 2])

'(1 2)

procedure

(sort/unique xs    
  <?    
  [#:key key    
  #:cache-keys? cache-keys?])  list?
  xs : list?
  <? : (-> any/c any/c boolean?)
  key : (-> any/c any/c) = identity
  cache-keys? : boolean? = #false
As sort, but discards duplicate elements.

Example:
> (sort/unique (list 2 7 1 8 2 8) <)

'(1 2 7 8)