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