;; distance-to-0 : posn --> number ;;to compute the distance of a-posn to the origin (define (distance-to-0 a-posn) ...)
(distance-to-0 (make-posn 0 5)) = 5 (distance-to-0 (make-posn 7 0)) = 7
(x2 + y2)1/2
(distance-to-0 (make-posn 3 4)) = 5 (distance-to-0 (make-posn 8 6)) = 10 (distance-to-0 (make-posn 5 12)) = 13
posn-x (which returns the x coordinate) posn-y (which returns the y coordinate)
(posn-x (make-posn 7 0)) = 7 (posn-y (make-posn 0 5)) = 5
(define a-posn (make-posn 7 0)) (posn-x a-posn) = 7 (posn-y a-posn) = 0
(+ (posn-y a-posn) 13 (+ (posn-x a-posn) (posn-y a-posn)) (+ (* (posn-x a-posn) (posn-x a-posn)) (* (posn-y a-posn) (posn-y a-posn)))
(define (distance-to-0 a-posn) (sqrt (+ (square (posn-x a-posn)) (square (posn-y a-posn)))))
(define (square x) (*x x))
(define-struct posn (x y))
(define-struct point (x y z))
(make-point 3 4 5)
(point-x (make-point 3 4 5)) = 3 (point-y (make-point 3 4 5)) = 4 (point-z (make-point 3 4 5)) = 5
Example - we manage several rock stars and we want to keep track of our data about them. We could write:
(define-struct star (last first instrument sales))
(make-star 'Friedman 'Dan 'ukelele 19004) (make-star 'Talcott 'Carolyn 'banjo 80000) (make-star 'Harper 'Robert 'bagpipe 27860)
(star-first E)to get his first name.
;;increment-sales : sales --> sales ;; to produce a star record like a-star ;;with 20000 more sales (define (increment-sales a-star) ...)
(increment-sales (make-star 'Abba 'John 'vocals 12200))
;; increment-sales star --> sales ;; to produce a star record like a-star with 20000 ;; more sales (define (increment-sales a-star) (make-star (star-last a-star) (make-star (star-first a-star) (make-star (star-instrument a-star) (make-star (+ (star-sales a-star) 20000)))
(make-posn 'Albert 'Meyer) (define (square x) (* x x)) (sqrt (+ (square (posn-x (make-posn 'Albert 'Meyer))) (square (posn-y (make-posn 'Albert 'Meyer)))))
;; posn is a structure (make-posn x y) ;; where x and y are numbers. ;;star is a structure (make-star last first instrument sales) ;;where last, first and instrument are ;;symbols and sales is a number.
Our record definition becomes:
(define -struct student (last first teacher))
(make-student 'findler 'kathy 'matthias)
;; process-student : student symbol --> ??? (define (process-student a-student a-teacher) ... (student-first a-student)... ... (student-last a-student)... ... (student-teacher a-student)...
;; process-student : student symbol -- > symbol (define (process-student a-student -ateacher) (cond [(eq? (student-teacher a-student) a-teacher) (student-last a-student)] [else 'none]))
Phase | Goal | Activity |
---|---|---|
Data Analysis & Design | To formulate a group of data definitions | Determine how many pieces of data describe important parts of the problem - then add a structure definition and a data definition |
Contract Header & Purpose | Specify program name, inputs & outputs, and purpose, and write a program header | Name the program, inputs, outputs, and specify its purpose. |
Examples | Characterize the input/output relationship via examples | Search the program for examples, validate these examples. |
Template | Formulate an outline for a groups of programs | Where parameters stand for compounds values, place selector expressions in the program. If the program is conditional, place selectors in each branch. |
Body | To define the program | Develop a Scheme expression that uses Scheme's primitive operation, other programs selector expressions and variables |
Test | To expose mistakes | Apply the program to the inputs of the examples, check that the outputs are as predicted. |
(define-struct student (last first teacher)) ;; A student is a structure ;; (make-student f l t) ; where f, l , and t are symbols
;;Subst-teacher : student symbol --> student
;; Purpose - to create a student structure with a new teacher name if the teacher's name matches 'Fritz
(subst-teacher (make-student 'Find 'Matthew 'Fritz) 'Elise) = (make-student 'Find 'Matthew 'Elise) (subst-teacher (make-student 'Find 'Matthew 'Amanda) 'Elise) = (make-student 'Find 'Matthew 'Amanda)
(define (process-student a-student a-teacher) ... (student-first a-student) ... ... (student-last a-student) ... ... (student-teacher a-student) ...
(define (subst-teacher a-student a-teacher) (cond [(eq? (student-teacher a-student) 'Fritz) (make-student (student-last a-student) (student-first a-student) a-teacher)] [else a-student]))
(subst-teacher (make-student 'Find 'Matthew 'Fritz) 'Elise) = (make-student 'Find 'Matthew 'Elise) (subst-teacher (make-student 'Find 'Matthew 'Amanda) 'Elise) = (make-student 'Find 'Matthew 'Amanda)