;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname lect03c) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))))) ;;;;; lect03a.rkt ; ; The design recipe: ; http://htdp.org/2003-09-26/Book/curriculum-Z-H-7.html#node_sec_4.4 ; --- per (abstract) data type: ; - data def'n ; - examples of data ; - template (case analysis, or field-extraction) ; --- per function: ; - contract, purpose, header ; - test-cases (check-expect) ; - complete function-body ; - for union types: cond-questions, 1 per data variant ; - for product types: inventory: ; extract each field, remind yourself of its type, list pertinent functions ; ;;; If a field is itself another complicated type (union or product), ; ;;; DO NOT handle it in the same function -- make a helper. ; - answer for each case ; - run the tests (trivial) ;=========== Lists ============ ; Data Def'n: ; A list-of-numbers is: ; - empty, ; OR ; - (cons num lon) ; num : number ; lon : list-of-numbers ; Examples of the data: empty (cons 3 empty) (cons 7 (cons 3 empty)) (cons 5 (cons 7 (cons 3 empty))) (define five-nums (cons 4 (cons 99 (cons 5 (cons 7 (cons 3 empty)))))) (first (cons 3 empty)) ; first: cons -> number ; rest : cons -> list-of-numbers ; NOT (cons 1 2 3 4 5) -- since cons can only have two inputs. ; NOT (cons 23 45) -- since the 2nd input to cons must be a lon, and 45 isn't. #| 'cons' is a constructor -- it takes in two things, and returns a 'constructed list'. It's as if we had said: (define-struct cons (first rest)) (make-cons 23 empty) |# ; To pull things *out* from a (constructed -- non-empty list): ; Use `first` and `rest`: ; first: cons -> number ; rest: cons -> lon ; How to extract the second thing in the list?? (define nums2 (cons 3 (cons 7 empty))) (first nums2) ; 7 ; How about: ;(first (rest (first nums2))) = (first (rest 3)) and (rest 3) is error (3 isn't a list) (first (rest nums2)) ; Exercise: ; Write a function which processes a list: ; Write a function which returns ...the sum of the numbers in a list. ; sum: list-of-number -> number ; (define (sum data) (cond [(empty? data) 0] [(cons? data) (+ (first data) (sum (rest data)))])) (check-expect (sum empty) 0) (check-expect (sum (cons 7 empty)) 7) (check-expect (sum (cons 3 (cons 7 empty))) 10) (check-expect (sum (cons 99 (cons 3 (cons 7 empty)))) 109) (check-expect (sum five-nums) 118) ; my-length : list-of-numbers -> number ; #;(define (length data) 17) (define (my-length data) (cond [(empty? data) 0] [(cons? data) (+ 1 (my-length (rest data)))])) ;; (check-expect (my-length empty) 0) (check-expect (my-length (cons 7 empty)) 1) (check-expect (my-length (cons 3 (cons 7 empty))) 2) (check-expect (my-length (cons 99 (cons 3 (cons 7 empty)))) 3) (check-expect (my-length five-nums) 5) ;;;;;;;;;;;;;;;;; #| hw: How to represent a spaceship, in racket? (or, in Java?) class Ship { double pointingAngle; // degrees clockwise, from North(up) //boolean thrust; double positionX, positionY; double speed; // pixels per tick } |#