(quick reminder in q & a session)
;; pi : list-of-numbers -> number ;; compute the product of the numbers on alon (define (pi alon) ...)
(pi (list 1 2 3))
(define alon (list 1 2 3)) (pi alon)
(define alon (list 1 2 3)) (pi (rest alon))
(define alon ...) (pi (rest alon))
;; pi : list-of-numbers -> number ;; compute the product of the numbers on alon (define (pi alon) (cond [(empty? alon) ...] [else ... (first alon) ... ... (pi (rest alon)) ...]))
That's why natural recursion works!
Like general scientists, we start with a simple model and refine it later:
[The Ptolemaeus Model of File Systems] _______________________ | | v | A directory is either | - empty | - (cons fyle directory) | | | ----------------- - (cons directory directory) | | | | ----------------- AThis data def has three self-references, but so what.fyle is a symbol (its name).
Now let's develop the function
;; exists? : dir symbol -> boolean ;; does name occur in a-dir? (define (exists? name a-dir) ...)
[The Newton Model of File Systems] (define-struct fyle (name size)) A directory is either - empty - (cons fyle directory) - (cons directory directory) A fyle is (make-fyle symbol number) ;; du : dir name -> number ;; du computes the disk usage of all the files in a-dir and its ;; subdirectories (define (du a-dir) ...)... the usual: examples, template, definitions ...
[The Einstein Model of File Systems] (define-struct dir (name size subs)) ____________________________________________________________________ | | v | A directory is (make-dir symbol number list-of-directories) | __________________________________________________ | | | | v | | A list-of-directories is either | | - empty | | - (cons d lod) | | where d is a directory and lod is a list-of-directories. | | | | | | | ---------------- | ---------------------------------------------------------
This is the first set of data definitions with mutual references (and self references). This is complicated. Let's make some examples. Let's make some examples:
(define MS (make-directory 'win3.1 300 empty)) (define Nxt (make-directory 'win 1 empty) (define Sys (make-directory 'sys 2 (cons Nxt (cons MS empty))))
[Continued]
Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |