Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 19.2.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
4 ;filter1 : predicate list-of-numbers
5 ;Filters out all numbers in a-lon where the predicate of the number evaluates to false. Retains all numbers that evaluate to true.
6 (define (filter1 predicate a-lon t)
7 (cond
8 [(empty? a-lon) empty]
9 [(predicate (first a-lon) t) (cons (first a-lon)
10 (filter1 predicate (rest a-lon) t))]
11 [else (filter1 predicate (rest a-lon) t)]))
13 (define-struct ir (name price))
15 ;An inventory-record (ir) is a structure
16 ;(make-ir n p)
17 ;where n is a symbol and p is a number.
19 ;An inventory is either
20 ;1. empty or
21 ;2. (cons ir inv)
22 ;where ir is an inventory record and inv is an inventory.
24 ;below-ir1 : number list-of-ir -> list-of-ir
25 ;Given n and a-loir, return a new list-of-ir with inventory records with prices below n.
27 (define (below-ir1 n a-loir)
28 (filter1 <ir a-loir n))
30 ;<ir : ir n -> boolean
31 ;Given an-ir and n, determine if the price of the ir is less than n.
33 (define (<ir an-ir n)
34 (< (ir-price an-ir) n))
36 ;Test Case
37 ;(below-ir1 10 (list (make-ir 'doll 8) (make-ir 'robot 12)))
39 #| Hand Evaluation
41 (below-ir1 10 (list (make-ir 'doll 8) (make-ir 'robot 12)))
42 (filter1 <ir (list (make-ir 'doll 8) (make-ir 'robot 12)) 10)
43 (cond
44 [false empty]
45 [(<ir (first (make-ir 'doll 8)) 10) (cons (first (list (make-ir 'doll 8) (make-ir 'robot 12)))
46 (filter1 predicate (rest (list (make-ir 'doll 8) (make-ir 'robot 12))) 10))]
47 [else (filter1 predicate (rest (list (make-ir 'doll 8) (make-ir 'robot 12))) 10)])
49 (<ir (first (make-ir 'doll 8)) 10)
50 (< 8 10)
51 true
53 (cond
54 [false empty]
55 [true (cons (make-ir 'doll 8)
56 (filter1 <ir (list (make-ir 'robot 12)) 10))]
57 [else (filter1 <ir (rest (list (make-ir 'doll 8) (make-ir 'robot 12))) 10)])
59 (cons (make-ir 'doll 8)
60 (filter1 <ir (list (make-ir 'robot 12)) 10))
62 (cond
63 [false empty]
64 [false (cons (first a-lon)
65 (filter1 predicate (rest a-lon) t))]
66 [else (filter1 <ir (empty) 10)])
68 (cons (make-ir 'doll 8)
69 empty)
71 |#
73 ;find : list-of-ir symbol -> boolean
74 ;Determine if a-loir contains an ir with the name a-symbol.
76 (define (find a-symbol a-loir)
77 (cons? (filter1 eq-ir? a-loir a-symbol)))
79 ;eq-ir? : ir a-symbol
80 ;Determine if an-ir has a-symbol for its name.
82 (define (eq-ir? an-ir a-symbol)
83 (symbol=? (ir-name an-ir) a-symbol))
85 (find 'doll (list (make-ir 'doll 8) (make-ir 'robot 12) (make-ir 'doll 13)))
87 (cons? (filter1 eq-ir? (list (make-ir 'doll 8) (make-ir 'robot 12) (make-ir 'doll 13)) 'doll))
89 (filter1 eq-ir? (list (make-ir 'doll 8) (make-ir 'robot 12) (make-ir 'doll 13)) 'doll)
91 (cons? (cons (first a-lon)
92 (filter1 eq-ir? (rest a-lon) t)))
94 true