Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-beginner-reader.ss" "lang")((modname 10.2.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
4 12687dd9 2023-08-04 jrmu ;Data Definition
5 12687dd9 2023-08-04 jrmu ;An ir (inventory record) is a structure
6 12687dd9 2023-08-04 jrmu ;(make-ir s n) where s is a symbol and n is a number.
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu (define-struct ir (name price))
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu ;Data definition
11 12687dd9 2023-08-04 jrmu ;An inventory is either
12 12687dd9 2023-08-04 jrmu ;1. an empty list or
13 12687dd9 2023-08-04 jrmu ;2. (cons ir inv) where ir is an inventory record (structure)
14 12687dd9 2023-08-04 jrmu ;and inv is an inventory.
15 12687dd9 2023-08-04 jrmu
16 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
17 12687dd9 2023-08-04 jrmu ;contains-doll? : inventory -> boolean
18 12687dd9 2023-08-04 jrmu ;Takes in an-inv (inventory) and searches to see if
19 12687dd9 2023-08-04 jrmu ;it contains 'doll. Return true or false.
20 12687dd9 2023-08-04 jrmu ;
21 12687dd9 2023-08-04 jrmu ;Template
22 12687dd9 2023-08-04 jrmu ;
23 12687dd9 2023-08-04 jrmu ;(define (contains-doll? an-inv)
24 12687dd9 2023-08-04 jrmu ; (cond
25 12687dd9 2023-08-04 jrmu ; [() ...]
26 12687dd9 2023-08-04 jrmu ; [() (first an-inv) (contains-doll? (rest an-inv))]))
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu (define (contains-doll? an-inv)
29 12687dd9 2023-08-04 jrmu (cond
30 12687dd9 2023-08-04 jrmu [(empty? an-inv) false]
31 12687dd9 2023-08-04 jrmu [(symbol=? (ir-name (first an-inv)) 'doll) true]
32 12687dd9 2023-08-04 jrmu [else (contains-doll? (rest an-inv))]))
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu (define list1 (cons (make-ir 'hat 55)
35 12687dd9 2023-08-04 jrmu (cons (make-ir 'match 25)
36 12687dd9 2023-08-04 jrmu (cons (make-ir 'tricycle 1840)
37 12687dd9 2023-08-04 jrmu (cons (make-ir 'jumprope 400)
38 12687dd9 2023-08-04 jrmu (cons (make-ir 'doll 150) empty))))))
39 12687dd9 2023-08-04 jrmu
40 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
41 12687dd9 2023-08-04 jrmu ;contains? : inventory symbol -> boolean
42 12687dd9 2023-08-04 jrmu ;Takes in an-inv (inventory) and searches to see if
43 12687dd9 2023-08-04 jrmu ;it contains keyword. Return true or false.
44 12687dd9 2023-08-04 jrmu
45 12687dd9 2023-08-04 jrmu (define (contains? an-inv keyword)
46 12687dd9 2023-08-04 jrmu (cond
47 12687dd9 2023-08-04 jrmu [(empty? an-inv) false]
48 12687dd9 2023-08-04 jrmu [(symbol=? (ir-name (first an-inv)) keyword) true]
49 12687dd9 2023-08-04 jrmu [else (contains? (rest an-inv) keyword)]))
50 12687dd9 2023-08-04 jrmu