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-beginner-reader.ss" "lang")((modname 11.3.3) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp")))))
4 ;;Produces an intenger between n and m-1
6 ;; random-n-m : integer integer -> integer
7 ;; ...
8 ;; Assume: n < m
9 (define (random-n-m n m)
10 (+ (random (- m n)) n))
11 ;
12 ;create-temps : number number number -> list-of-numbers
13 ;Creates a list-of-numbers consisting of n numbers between low and high.
14 ;Example
15 ;(create-temps 0 low high)
16 ;empty
17 ;(create-temps 1 low high)
18 ;(cons (random-n-m low (+ high 1)) empty)
19 ;(create-temps 2 low high)
20 ;(cons (random-n-m low (+ high 1)) (cons (random-n-m low (+ high 1)) empty))
21 ;
22 ;Template
23 ;
24 ;(define (create-temps n low high)
25 ; (cond
26 ; [(zero? n) ...]
27 ; [(>= n 1) ...]
28 ; [else (error 'create-temps "expected 1st arg: natural number")]))
30 (define (create-temps n low high)
31 (cond
32 [(zero? n) empty]
33 [(>= n 1) (cons (random-n-m low (+ high 1)) (create-temps (sub1 n) low high))]
34 [else (error 'create-temps "expected 1st arg: natural number")]))
36 ;Data Definition
37 ;A list-of-temperatures is either
38 ;1. an empty list or
39 ;2. (cons t los) where t is a number and los is a list-of-temperatures.
41 ;Header, Purpose, Contract
42 ;check-range1? : list-of-temperatures -> boolean
43 ;Given a-list, returns true if the list of temperatures are within the range
44 ;low and high (endpoints included as true).
45 ;
46 (define (check-range1? a-list low high)
47 (cond
48 [(empty? a-list) true]
49 [(cons? a-list) (cond
50 [(and
51 (number? (first a-list))
52 (>= (first a-list) low)
53 (<= (first a-list) high))
54 (check-range1? (rest a-list) low high)]
55 [(number? (first a-list)) false]
56 [else
57 (error 'check-range1? "list should be composed of numbers")])]
58 [else (error 'check-range1? "expected 3 arg: list number number")]))
60 ;create-prices : natural-number number number -> list-of-numbers
61 ;Given a natural-number n, creates a list of n prices
62 ;between low and high rounded to the nearest
63 ;dime ($0.10). Creates this list by
64 ;making the function call random-dime.
65 ;
66 ;Examples
67 ;(create-prices 0 low high)
68 ;empty
69 ;(create-prices 1 low high)
70 ;(cons (random-dime low high) empty)
71 ;(create-prices 2 low high)
72 ;(cons (random-dime low high)
73 ; (cons (random-dime low high) empty))
74 ;
75 (define (create-prices n low high)
76 (cond
77 [(zero? n) empty]
78 [(>= n 1) (cons (random-dime low high) (create-prices (sub1 n) low high))]
79 [else (error 'create-prices "expected 1st arg: natural-number")]))
81 ;random-dime : number number -> number
82 ;Given low and high, creates a number (decimal) between low and high
83 ;rounded to the nearest dime. It does so by choosing a random integer
84 ;between 0 and (high-low)/0.1, inclusive. So for example,
85 ;for low=0 and high=1, it chooses a number between 0 and 10 (inclusive).
86 ;It then multiplies this number by 0.1, the value of a dime.
87 ;This function thus returns us a number between $0.00 and $1.00,
88 ;rounded to the nearest dime.
90 (define (random-dime low high)
91 (*
92 (random-n-m 0
93 (+ (/ (- high low) 0.1) 1))
94 0.1))
96 ;
97 ;dollar-store? : list-of-numbers -> boolean
98 ;Checks a-list to see if the prices are below 1. Returns true
99 ;if the statement is true, false otherwise.
101 ;Template
102 ;(define (dollar-store? a-list)
103 ; (cond
104 ; [(empty? a-list) ...]
105 ; [(cons? a-list) (first a-list) (rest a-list)]))
107 (define (dollar-store? a-list)
108 (cond
109 [(empty? a-list) true]
110 [(cons? a-list) (cond
111 [(and
112 (number? (first a-list))
113 (< (first a-list) 1))
114 (dollar-store? (rest a-list))]
115 [else false])]))
117 ;eval-until-true : natural-number number number -> boolean
118 ;Keep evaluating (dollar-store? (create-prices n low high))
119 ;until it returns true.
121 (define (eval-until-true n low high)
122 (cond
123 [(dollar-store? (create-prices n low high)) true]
124 [else (eval-until-true n low high)]))