Lecture 7: Using the Recipes



  1. A minor point:
    value = 
        0 | +1 | -1 | ...
      | #t | #f 
      | 'hello | 'world 
      | empty
      | list
    
    list = 
        empty
      | (cons value list)
    
    All constants are values. And lists are values.

    Some strange examples

    (cons (cons 1 (cons 2 empty)) empty)
    
    How many items are in this list?
  2. Given: the position of an object (a Cartesian point), its velocity (a vector), and a time. Question: develop a program that produces the new position.

    Is this about core knowledge or computational knowledge?

      * t = 0 
       \
        \
         \
          v
           * t = 1 
           ...       
    

    Core knowledge: Multiply velocity by time, add to position. Even if that means mulitplying vectors etc.


  3. Given: a sequence of names [Celsius]. Eliminate all those over 100.

    What form of data? Lists of numbers. Contract. Examples. Template.

    #|
       elim : (listof number) -> (listof number)
    
       Purpose: produce a list of numbers from the input by removing all those greater than 100
    
       Data Def.: list-of-number = empty | (cons number list-of-number)
    
       Examples: 
        (elim (list 20 30 100 40 20 30 100)) = (list 20 30 40 20 30)
        (elim empty) = empty
    
       Template: 
         (define (elim alon)
           (cond
    	 ((empty? alon) ...)
    	 (else ... (first alon) ... (elim (rest alon)) ...)))
         
    |#
    (define (elim alon)
      (cond
        ((empty? alon) empty)
        (else (cond
    	    ((> (first alon) 100) (elim (rest alon)))
    	    (else (cons (first alon) (elim (rest alon))))))))
    

  4. The above looks silly. Who would want to remove numbers from a list using a computer program? Okay, let's look at the following example:

    Given a table of names, salaries, and ranks.

    Name Age Salary
    Man 23 120000
    BigMan 46 800000
    Little 19 30000
    Woman 32 300000
    Kiddo 18 20000

    Develop a program that consumes a table and produces one with all those rows removed where age is below some threshold A.

    #|
       project : db number[A] -> db
    
       Purpose: produce a db from the input by including only those whose age
         is greater than A
    
       Data def: 
    
       db is a list of db-records: db = empty | (cons db-record db)
    
       db-record: 
       (define-struct record (name age salary))
    
       A db record is (make-record symbol a s)
         where a is an integer between MIN-AGE and MAX-AGE (incl)
         and s is an integer between MIN-SALARY and MAX-SALARY (incl)
    
       Examples: 
        (project DB1 
        
        (project empty 100) = empty
    
       Template: 
         (define (project alon)
           (cond
    	 ((empty? alon) ...)
    	 (else ... (first alon) ... (project (rest alon)) ...)))
         
    |#
    (define (project db)
      (cond
        ((empty? db) empty)
        (else (cond
    	    ((<= (record-age (first db)) A) (project (rest db)))
    	    (else (cons (first db) (project (rest db))))))))
    
    (define-struct record (name age salary))
    
    (define DB1
      (cons
      (make-record 'Man    23    120000 )
      (cons
      (make-record 'BigMan  46   800000 )
      (cons
      (make-record 'Little  19    30000 )
      (cons 
      (make-record 'Woman   32   300000 )
      (cons 
      (make-record 'Kiddo   18    20000 )
      empty))))))
    




Matthias Felleisen This page was generated on Fri Apr 9 09:17:38 CDT 1999.