On this page:
for/  hash!
for*/  hash!
for/  hasheq!
for*/  hasheq!
for/  hasheqv!
for*/  hasheqv!
for/  dict
for*/  dict
for/  dict!
for*/  dict!
for/  filter
for*/  filter
for/  filter-lists
for*/  filter-lists
for/  append
for*/  append
for/  append-lists
for*/  append-lists
for/  partition
for*/  partition
for/  partition-lists
for*/  partition-lists
for/  fold/  lists
for*/  fold/  lists
for/  fold/  filter-lists
for*/  fold/  filter-lists
for/  fold/  append-lists
for*/  fold/  append-lists
define/  for/  fold
define/  for*/  fold
define/  for/  fold/  lists
define/  for*/  fold/  lists
define/  for/  lists
define/  for*/  lists
define/  for/  filter-lists
define/  for*/  filter-lists
define/  for/  append-lists
define/  for*/  append-lists
define/  for/  partition
define/  for*/  partition
define/  for/  partition-lists
define/  for*/  partition-lists
define/  for/  fold/  filter-lists
define/  for*/  fold/  filter-lists
define/  for/  fold/  append-lists
define/  for*/  fold/  append-lists
7.1

7.4 mischief/for: For Loops

 (require mischief/for) package: mischief

syntax

(for/hash! for-clauses . for-body)

syntax

(for*/hash! for-clauses . for-body)

syntax

(for/hasheq! for-clauses . for-body)

syntax

(for*/hasheq! for-clauses . for-body)

syntax

(for/hasheqv! for-clauses . for-body)

syntax

(for*/hasheqv! for-clauses . for-body)

Loops for creating mutable hash tables, by analogy with for/hash and similar forms.

Examples:
> (define keys '(one two three four))
> (define vals '(1 2 3 4))
> (for/hash! {[k (in-list keys)] [v (in-list vals)]} (values k v))

'#hash((one . 1) (three . 3) (four . 4) (two . 2))

> (for/hasheq! {[k (in-list keys)] [v (in-list vals)]} (values k v))

'#hasheq((one . 1) (three . 3) (two . 2) (four . 4))

> (for/hasheqv! {[k (in-list keys)] [v (in-list vals)]} (values k v))

'#hasheqv((one . 1) (three . 3) (two . 2) (four . 4))

syntax

(for/dict init-expr for-clauses . for-body)

syntax

(for*/dict init-expr for-clauses . for-body)

syntax

(for/dict! init-expr for-clauses . for-body)

syntax

(for*/dict! init-expr for-clauses . for-body)

Loops for creating immutable and mutable dictionaries. They work by analogy with for/hash and similar forms, with the addition of an explicit init-expr specifying a dictionary to add the bindings to.

Examples:
> (for/dict '([a one] [b two])
      {[k (in-list '(b c))]
       [v (in-list '("B" "C"))]}
    (values k v))

'((a one) (b . "B") (c . "C"))

> (define vec (vector 0 0 0 0))
> (for/dict! vec {[i (in-range 4)]}
    (values i (* i i)))

'#(0 1 4 9)

> vec

'#(0 1 4 9)

syntax

(for/filter for-clauses . for-body)

syntax

(for*/filter for-clauses . for-body)

syntax

(for/filter-lists {list-id ...} for-clauses . for-body)

syntax

(for*/filter-lists {list-id ...} for-clauses . for-body)

Loops for creating lists and discarding #false elements.

Examples:
> (for*/filter {[vowel (in-list '(a e i o u))]
                [word (in-list '([c a t] [b i r d] [m o u s e]))]}
    (memq vowel word))

'((a t) (e) (i r d) (o u s e) (u s e))

> (define (root x)
    (define y (sqrt x))
    (and (exact-integer? y) y))
> (define (half x)
    (define y (/ x 2))
    (and (exact-integer? y) y))
> (for/filter-lists {roots halves} {[i (in-range 10)]}
    (values (root i) (half i)))

'(0 1 2 3)

'(0 1 2 3 4)

syntax

(for/append for-clauses . for-body)

syntax

(for*/append for-clauses . for-body)

syntax

(for/append-lists {list-id ...} for-clauses . for-body)

syntax

(for*/append-lists {list-id ...} for-clauses . for-body)

Loops for appending lists.

Examples:
> (for/append {[str (in-list '("cat" "a" "log"))]}
    (string->list str))

'(#\c #\a #\t #\a #\l #\o #\g)

> (for/append-lists {evens odds} {[i (in-range 10)]}
    (partition even? (build-list i add1)))

'(2 2 2 4 2 4 2 4 6 2 4 6 2 4 6 8 2 4 6 8)

'(1 1 1 3 1 3 1 3 5 1 3 5 1 3 5 7 1 3 5 7 1 3 5 7 9)

syntax

(for/partition for-clauses . for-body)

syntax

(for*/partition for-clauses . for-body)

syntax

(for/partition-lists {[yes-id no-id] ...} for-clauses . for-body)

syntax

(for*/partition-lists {[yes-id no-id] ...} for-clauses . for-body)

Loops for partitioning results into two categories.

Examples:
> (for/partition {[{i s} (in-dict (vector 'a 'e 'i 'o 'u))]}
    (values (even? i) s))

'(a i u)

'(e o)

> (for/partition-lists {[evens odds] [lefts rights]}
      {[{i s} (in-dict (vector 'a 'e 'i 'o 'u))]}
    (values (even? i) i s))

'(0 2 4)

'(a i u)

'(1 3)

'(e o)

syntax

(for/fold/lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(for*/fold/lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(for/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(for*/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(for/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(for*/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)

Loops that combine folded results with accumulated lists.

Examples:
> (for/fold/lists
      {[len 0]}
      {syms}
      {[str (in-list '("one" "two" "three"))]}
    (values
      (+ len (string-length str))
      (string->symbol str)))

11

'(one two three)

> (define table (hash 'a "apple" 'b "banana"))
> (for/fold/filter-lists
      {[longest 0]}
      {hits}
      {[key (in-list '(a b c))]
       #:when (dict-has-key? table key)}
    (define str (dict-ref table key))
    (values
      (max longest (string-length str))
      str))

6

'("apple" "banana")

> (for/fold/append-lists
      {[count 0]}
      {chars}
      {[word (in-list '(cat a log))]}
    (values
      (add1 count)
      (string->list (symbol->string word))))

3

'(#\c #\a #\t #\a #\l #\o #\g)

syntax

(define/for/fold {[x e] ...} for-clauses . for-body)

syntax

(define/for*/fold {[x e] ...} for-clauses . for-body)

syntax

(define/for/fold/lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(define/for*/fold/lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(define/for/lists {x ...} for-clauses . for-body)

syntax

(define/for*/lists {x ...} for-clauses . for-body)

syntax

(define/for/filter-lists {x ...} for-clauses . for-body)

syntax

(define/for*/filter-lists {x ...} for-clauses . for-body)

syntax

(define/for/append-lists {x ...} for-clauses . for-body)

syntax

(define/for*/append-lists {x ...} for-clauses . for-body)

syntax

(define/for/partition {x y} for-clauses . for-body)

syntax

(define/for*/partition {x y} for-clauses . for-body)

syntax

(define/for/partition-lists {[x y] ...} for-clauses . for-body)

syntax

(define/for*/partition-lists {[x y] ...} for-clauses . for-body)

syntax

(define/for/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(define/for*/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(define/for/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)

syntax

(define/for*/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)

Definitions based on loops.

Examples:
> (define/for/fold {[vowels 0]} {[c (in-string "beautiful")]}
    (+ vowels (if (memv c (list #\a #\e #\i #\o #\u)) 1 0)))
> vowels

5