numbers | numeric information |
booleans | true and false |
symbols | representing symbol information (similar to characterinformation) |
structures | compound data |
We can express these points using:
A (make-posn 6 1) D 2 B (make-posn 1 5) E 3 C 1
(number ? 34) >true
(define-struct posn (x y)) (define-struct star (last first dob ssn)) (define-struct airplane (kind max-speed max-load price))
(number? (make-posn 2 3)) (number? (+ 12 10)) (posn 23 ) (posn? (make-posn 23 3)) (star? make-posn 23 3)
;;distance-to-0 ;;to compute the distance from a-point to the origin (define (distance-to-0 a-point) ...)
(define (distance-to-0 a-point) (cond [(number? a-point) ...] [(posn? a-point) ...]))
(define (distance-to-0 a-point) (cond [(number? a-point) ...] [(posn? a-point) ... (posn-x a-point)... (posn-y a-point)...]))
(define (distance-to-0 a-point) (cond [(number? a-point) a-point] [(posn? a-point)(sqrt (+ (square(posn-x a-point)) (square (posn-y a-point))))]))
(define-struct square (nw length) (define-struct circle (center radius)
;; perimeter : shape --> number ;; to compute the perimeter of a-shape (define (perimeter a-shape) (cond [(square? a-shape) ...] [ (circle? a-shape) ...]))
;; perimeter : shape --> number ;; to compute the perimeter of a-shape (define (perimeter a-shape) (cond [(square? a-shape) ... (square-nw a-shape) ... (square-length a-shape) ...] [ (circle? a-shape) ... (circle-center a-shape) ... (circle-radius a-shape ...)]))
;; perimeter : shape --> number ;; to compute the perimeter of a-shape (define (perimeter a-shape) (cond [(square? a-shape) (* (square-length a-shape) 4)] [ (circle? a-shape) (* (* 2 (circle-radius a-shape))pi)]))
(define-struct circle (center radius)) (define-struct square (nw length)) ;;A shape is either ;;1. a structure: (make-circle p s) ;;2. a structure: (make-square p s) ;; where p is a position, as a number
(define (f a-shape) (cond [(square? a-shape) ... (square-nw a-shape) ... (square-length a-shape)...] [(circle? a-shape) ...(circle-center a-shape) ... (circle-radius a-shape)... ]))
;; perimeter : shape --> number ;; to compute the perimeter of a-shape (define (perimeter a-shape) (cond [(square? a-shape) (*(square-length a-shape) 4)] [(circle? a-shape) (*(*circle-radius a-shape)2)pi)] ) )
(make-circle p s) ;; p is a posn & s is a number (make-square p s) ;; p is a posn & s is a number
;; f : shape --> ? (define (f a-shape) (cond [(circle? a-shape) (f-for-a-circle a-shape)] [(square? a-shape) (f-for-a-square a-shape)] ) )
;; perimeter : shape --> number (define (perimeter a-shape) (cond [(circle? a-shape)(circ-perimeter a-shape)] [(square? a-shape) (sq-perimeter a-shape)] ) ) ;; circ-perimeter : shape --> number (define (circ-perimeter a-shape) (* (* (circle-radius a-shape) 2)pi)) ;; sq-perimeter : shape --> number (define (sq-perimeter a-shape) (* (square-length a-shape) 4))
;; area-of-disk : number --> number ;; to compute the area of a disk with radius r (define (area-of-disk r) (* 3.14 (* r r)))
(area-of-disk 'five)
;; checked-area-of-disk : number --> number (define (checked-area-of-disk v) (cond [(number? v) (area-of-disk v)] [(boolean? v) error 'checked area-of-disk "number expected"] [(symbol? v) error 'checked area-of-disk "number expected"] [(struct? v) error 'checked area-of-disk "number expected"]))
(define (checked-area-of-disk v) (cond [(number? v) (area-of-disk v)] [else error 'checked area-of-disk "number expected"] ) )