[plt-scheme] How to fold it

From: wooks (wookiz at hotmail.com)
Date: Fri May 15 23:04:01 EDT 2009

;;slide :: [*] -> [[*]]
;;Given a list like '(0 1 2 3 4) produces '((0) (0 1) (0 1 2) (0 1 2
3) (01 2 3 4))
(define slide
  (lambda (a-list)
    (cond
      [(empty? a-list) empty]
      [else (cons (list (first a-list))
                  (map (lambda (x) (cons (first a-list) x))
                       (slide (rest a-list))))])))

This code is in the spirit of HTDP 12.4.2. The map prefixes the
inductive case with the first of the list and

        cons (list (first a-list))

was what was required to get from what I had to what I wanted.

It looks like something that should be foldable. How to get there?



Posted on the users mailing list.