Next: , Previous: , Up: stacks   [Index]


36.4 Folding over stacks

The following syntactic bindings are exported by the library (vicare containers stacks). The syntactic bindings whose name is prefixed with $ are unsafe operations: they do not validate their arguments before accessing them.

Function: stack-fold-left kons knil stack
Function: $stack-fold-left kons knil stack

Analogous to fold-left for lists. Fold the procedure kons over the objects from stack, starting from the top.

(define D
  (stack 0 1 2 3 4 5))

(stack-fold-left (lambda (knil obj)
                   (cons obj knil))
  '() D)
⇒ (5 4 3 2 1 0)
Function: stack-fold-right kons knil stack
Function: $stack-fold-right kons knil stack

Analogous to fold-right for lists. Fold the procedure kons over the objects from stack, starting from the bottom.

(define D
  (stack 0 1 2 3 4 5))

(stack-fold-right (lambda (obj knil)
                    (cons obj knil))
  '() D)
⇒ (0 1 2 3 4 5)