Comp 200 Lecture
more scheme

  1. 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.
    ;;
    
  2. If the input is a list or tree (or, a structure), you can write the code to
    1. ask what type of list or tree, e.g. (if (unknown? in-tree) ...), and
    2. 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).
  3. 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.
  4. read
  5. help-desk (print, nested-if)
  6. association list
  7. reminder: writing loops E.g. as a dungeon:
    (define (go-dungeon cmd curr-room)
      (go-dungeon (read) (do-command cmd curr-room)))
    
  8. 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)))))
    
  9. local