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-intermediate-reader.ss" "lang")((modname |21.2|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
4 12687dd9 2023-08-04 jrmu ;move-all : (listof posn) -> (listof posn)
5 12687dd9 2023-08-04 jrmu ;Given alop, translate every posn to the right by 3.
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu ;move-x-3 : posn -> posn
8 12687dd9 2023-08-04 jrmu ;Given aposn, translate to the right by 3.
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu (define (move-all alop)
11 12687dd9 2023-08-04 jrmu (local ((define (move-all alop)
12 12687dd9 2023-08-04 jrmu (map move-x-3 alop))
13 12687dd9 2023-08-04 jrmu (define (move-x-3 aposn)
14 12687dd9 2023-08-04 jrmu (make-posn (+ 3 (posn-x aposn))
15 12687dd9 2023-08-04 jrmu (posn-y aposn))))
16 12687dd9 2023-08-04 jrmu (move-all alop)))
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu (define-struct toy (name price))
19 12687dd9 2023-08-04 jrmu
20 12687dd9 2023-08-04 jrmu ;A toy is a structure
21 12687dd9 2023-08-04 jrmu ;(make-toy n p)
22 12687dd9 2023-08-04 jrmu ;where n is a symbol and p is a number.
23 12687dd9 2023-08-04 jrmu ;
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu
26 12687dd9 2023-08-04 jrmu ;eliminate-exp : (listof toy) number -> (listof toy)
27 12687dd9 2023-08-04 jrmu ;Given alot, return all those (listof toy) whose price is below ua.
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu ;local
30 12687dd9 2023-08-04 jrmu ;eliminate-exp : (listof toy) -> (listof toy)
31 12687dd9 2023-08-04 jrmu ;Given alot, return all those (listof toy) whose price is below ua.
32 12687dd9 2023-08-04 jrmu ;
33 12687dd9 2023-08-04 jrmu ;expensive : toy -> boolean
34 12687dd9 2023-08-04 jrmu ;Given ua, return true if the atoy has a price below ua.
35 12687dd9 2023-08-04 jrmu
36 12687dd9 2023-08-04 jrmu (define (eliminate-exp alot ua)
37 12687dd9 2023-08-04 jrmu (local ((define (eliminate-exp alot)
38 12687dd9 2023-08-04 jrmu (filter expensive? alot))
39 12687dd9 2023-08-04 jrmu (define (expensive? atoy)
40 12687dd9 2023-08-04 jrmu (< (toy-price atoy) ua)))
41 12687dd9 2023-08-04 jrmu (eliminate-exp alot)))
42 12687dd9 2023-08-04 jrmu
43 12687dd9 2023-08-04 jrmu ;recall : symbol (listof symbol) -> (listof symbol)
44 12687dd9 2023-08-04 jrmu ;Consumes the name of a toy, called ty, and a list of names, called lon, and produces a list of names that contains all components of lon with the exception of ty
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu (define (recall ty lon)
47 12687dd9 2023-08-04 jrmu (local ((define (recall lon)
48 12687dd9 2023-08-04 jrmu (filter not-equal lon))
49 12687dd9 2023-08-04 jrmu (define (not-equal name)
50 12687dd9 2023-08-04 jrmu (not (symbol=? ty name))))
51 12687dd9 2023-08-04 jrmu (recall lon)))
52 12687dd9 2023-08-04 jrmu
53 12687dd9 2023-08-04 jrmu selection : (listof symbol) (listof symbol) -> (listof symbol)
54 12687dd9 2023-08-04 jrmu
55 12687dd9 2023-08-04 jrmu (define (selection list1 list2)
56 12687dd9 2023-08-04 jrmu (cond
57 12687dd9 2023-08-04 jrmu [(empty? list2) empty]
58 12687dd9 2023-08-04 jrmu [(filter in-other-list? (first list2))
59 12687dd9 2023-08-04 jrmu (cons (first list2)
60 12687dd9 2023-08-04 jrmu (selection list1 (rest list2)))]
61 12687dd9 2023-08-04 jrmu [else (selection list1 (rest list2))]))
62 12687dd9 2023-08-04 jrmu
63 12687dd9 2023-08-04 jrmu in-other-list? : symbol -> boolean
64 12687dd9 2023-08-04 jrmu Given name, determine if it occurs in the other list.
65 12687dd9 2023-08-04 jrmu
66 12687dd9 2023-08-04 jrmu (filter listequal list1)
67 12687dd9 2023-08-04 jrmu listequal
68 12687dd9 2023-08-04 jrmu = (first list2) (first list1)
69 12687dd9 2023-08-04 jrmu = (first list2) (second list1)
70 12687dd9 2023-08-04 jrmu ...
71 12687dd9 2023-08-04 jrmu = (first list2) (first list1)
72 12687dd9 2023-08-04 jrmu
73 12687dd9 2023-08-04 jrmu
74 12687dd9 2023-08-04 jrmu ;selection : (listof symbol) (listof symbol) -> (listof symbol)
75 12687dd9 2023-08-04 jrmu ;selection, which consumes two lists of names and selects all those from the second one that are also on the first.
76 12687dd9 2023-08-04 jrmu
77 12687dd9 2023-08-04 jrmu (define (selection list1 list2)
78 12687dd9 2023-08-04 jrmu (cond
79 12687dd9 2023-08-04 jrmu [(empty? list2) empty]
80 12687dd9 2023-08-04 jrmu [(in-other-list? (first list2) list1) (cons (first list2)
81 12687dd9 2023-08-04 jrmu (selection list1 (rest list2)))]
82 12687dd9 2023-08-04 jrmu [else (selection list1 (rest list2))]))
83 12687dd9 2023-08-04 jrmu
84 12687dd9 2023-08-04 jrmu (define (in-other-list? an-item a-list)
85 12687dd9 2023-08-04 jrmu (cond
86 12687dd9 2023-08-04 jrmu [(empty? a-list) false]
87 12687dd9 2023-08-04 jrmu [(equal? (first a-list) an-item) true]
88 12687dd9 2023-08-04 jrmu [else (in-other-list? an-item (rest a-list))]))
89 12687dd9 2023-08-04 jrmu
90 12687dd9 2023-08-04 jrmu in-other-list? symbol -> boolean
91 12687dd9 2023-08-04 jrmu Given name and list2, determine if name occurs anywhere in list2.
92 12687dd9 2023-08-04 jrmu
93 12687dd9 2023-08-04 jrmu
94 12687dd9 2023-08-04 jrmu
95 12687dd9 2023-08-04 jrmu #|
96 12687dd9 2023-08-04 jrmu
97 12687dd9 2023-08-04 jrmu Exercise 21.2.3. Here is the version of filter that DrScheme provides:
98 12687dd9 2023-08-04 jrmu
99 12687dd9 2023-08-04 jrmu ;; filter : (X -> boolean) (listof X) -> (listof X)
100 12687dd9 2023-08-04 jrmu ;; to construct a list of X from all those items on alon
101 12687dd9 2023-08-04 jrmu ;; for which predicate? holds
102 12687dd9 2023-08-04 jrmu (define (filter predicate? alon)
103 12687dd9 2023-08-04 jrmu (cond
104 12687dd9 2023-08-04 jrmu [(empty? alon) empty]
105 12687dd9 2023-08-04 jrmu [else (cond
106 12687dd9 2023-08-04 jrmu [(predicate? (first alon))
107 12687dd9 2023-08-04 jrmu (cons (first alon) (filter predicate? (rest alon)))]
108 12687dd9 2023-08-04 jrmu [else (filter predicate? (rest alon))])]))
109 12687dd9 2023-08-04 jrmu 1. eliminate-exp, which consumes a number, ua, and a list of toy structures (containing name and price) and produces a list of all those descriptions whose price is below ua;
110 12687dd9 2023-08-04 jrmu 2. recall, which consumes the name of a toy, called ty, and a list of names, called lon, and produces a list of names that contains all components of lon with the exception of ty;
111 12687dd9 2023-08-04 jrmu 3. selection, which consumes two lists of names and selects all those from the second one that are also on the first.
112 12687dd9 2023-08-04 jrmu
113 12687dd9 2023-08-04 jrmu |#