;; 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 testprep1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;;Review session 2/3/14 ;target-heart-rate: Number->Number ; Computes the age of the user and determines ; the target heart rate (define (target-heart-rate age) (* (- 220 age) .75)) (check-expect (target-heart-rate 120) 75) ;;; Problem 2 ;; (my-max (* 3 4) 7) ;; 1) arith: ;; (my-max (12) 7) ;; 2) plug-in: ;; (cond ;; [(< 12 7) 7] ;; [else 12]) ;; 3) arith: ;; (cond ;; [(false) 7] ;; [else 12]) ;; 4) cond: ;; (cond [else 12]) ;; 5) cond: ;; 12 (define-struct stock (company num-shares share-price)) ;;; A Holding is one of: ;;; -(make-stock String Number Number) ;;; -Number ;;; where a simple number means cash, in US dollars, ;;; and a stock structure represents a block of shares ;;; in some company. ; stock-temp: Holding -> Any #;(define (stock-temp a-stock) (cond [(number? a-stock) ...] [(stock? a-stock) ... (stock-company a-stock) ... (stock-num-shares a-stock) ... (stock-share-price a-stock)...])) ;; A List-Of-Numbers is one of: ;; -empty ;; -(cons Number List-Of-Numbers) (define ex1 empty) (define ex2 (cons 1 empty)) (define ex3 (cons 0 (cons 1 (cons -1 empty)))) ; lon-temp: List-Of-Numbers -> Any #; (define (lon-temp a-lon) (cond [(empty? a-lon) ...] [(cons? a-lon) ...(first a-lon) ...(lon-temp (rest a-lon))])) ;; extract-positives : list-of-number -> list-of-number ;; consumes a list of numbers and produces a list of all ;; the positive numbers from the list ;; (consider 0 positive) (check-expect (extract-positives ex1) empty) (check-expect (extract-positives ex2) (cons 1 empty)) (check-expect (extract-positives ex3) (cons 0 (cons 1 empty))) (define (extract-positives a-lon) (cond [(empty? a-lon) empty] [(>= (first a-lon) 0) (cons (first a-lon) (extract-positives (rest a-lon)))] [else (extract-positives (rest a-lon))])) ;; --------------------------------------------------------- ;; A List-Of-Numbers is one of: ;; -empty ;; -(cons Number List-Of-Numbers) ; sum-list: List-Of-Number -> Number ; Add up all the Numbers in a List-Of-Number (check-expect (sum-list empty) 0) (check-expect (sum-list (cons 4 (cons 23 (cons -1 empty)))) 26) (define (sum-list a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (+ (first a-lon) (sum-list (rest a-lon)))])) ;; THIS IS HARD ;; IF YOU CAN'T DO THIS DON'T WORRY ;; BUT IF YOU CAN, GREAT! ;; But try following it - and reproducing it. ;; It's great practice. ;; BUT UNDERSTANDING THE PROBLEMS ABOVE ARE MORE ;; IMPORTANT ;; DESIGN RECIPE ;; DESIGN RECIPE ;; DESIGN RECIPE ;; :) (define-struct person(age name)) ;; A Person is a (make-person Number String) (define ash (make-person 18 "Ashwin")) (define batman (make-person 35 "Bruce")) (define amal (make-person 22 "Amal")) (define leena (make-person 21 "Leena")) ; person-temp : Person -> ? #;(define (person-temp a-person) ...(person-age a-person) ...(person-name a-person)...) ;; Goal: Sort people by age (ascending) ;; A List-of-People is one of: ;; - empty ;; - (cons Person List-of-People) (define l1 empty) (define l2 (cons ash (cons batman (cons amal (cons leena l1))))) (define l3 (cons ash (cons leena (cons amal (cons batman empty))))) ;; list-of-people-temp : List-of-People -> ? #;(define (list-of-people-temp alist) (cond [(empty? alist) ...] [else (... (person-temp (first alist)) ... (list-of-people-temp (rest alist)) ...)])) ;; age-sort : List-of-People -> List-of-People ;; Sorts the list by age (ascending) (check-expect (age-sort l1) l1) (check-expect (age-sort l2) l3) (check-expect (age-sort l3) l3) (define (age-sort alist) (cond [(empty? alist) empty] [else (insert (first alist) (age-sort (rest alist)))])) ;; insert : Person x List-of-People -> List-of-People ;; Inserts a person in a sorted list of people at the proper place (check-expect (insert batman empty) (cons batman empty)) (check-expect (insert amal (cons leena (cons batman empty))) (cons leena (cons amal (cons batman empty)))) (define (insert person alist) (cond [(empty? alist) (cons person empty)] [(< (person-age person) (person-age (first alist))) (cons person alist)] [else (cons (first alist) (insert person (rest alist)))]))