- For each function, start by first typing a comment
with exactly what type of inputs it takes, and what types
of output it produces.
;; sort: list-of-symbols --> number
;; Return the length of the input list.
;;
-
If the input is a list or tree (or, a structure),
you can write the code to
- ask what type of list or tree, e.g.
(if (unknown? in-tree) ...)
,
and
- select out the subparts of the list/tree/structure
(e.g.
first/rest
for lists,
child-ma/child-pa/child-name
for FamTrees,
and album-name/album-price
for albums).
-
The built-in scheme function
define-structure
does
the drudge-work of representing a child as (say) a list of three things,
and writing the accessor functions child-ma
, etc.
Try:
(define-structure (child ma pa name))
;
this doesn't return a value, but instead it has DrScheme automatically
create the functions make-child, child-ma, child-pa, child-name
and child?
for you.
read
- help-desk
(print, nested-if)
- association list
- reminder: writing loops
E.g. as a dungeon:
(define (go-dungeon cmd curr-room)
(go-dungeon (read) (do-command cmd curr-room)))
-
generalized sort
(define (insert itm lst)
(if (empty? lst)
(cons itm empty)
(if (> itm (first lst)
(cons (first lst) (insert itm (rest lst)))
(cons itm lst))))
(define (insert-sort lst)
(if (empty? lst)
empty
(insert (first lst) (insert-sort (rest lst)))))
-
local