HtDP Solution Set

Section 9


#|
Data Definitions

A List-of-number is one of
- empty
- (cons Number List-of-number)

A List-of-boolean is one of
- empty
- (cons Boolean List-of-boolean)

A Non-empty-list-of-string is one of
- (cons String empty)
- (cons String Non-empty-list-of-string)

A List-of-string is one of
- empty
- (cons String List-of-string)

A List-of-Symbol is one of
- empty
- (cons Symbol List-of-Symbol)
|#



Problem 1 (Solution):
;;between? : Number Number Number -> Boolean
; -- is the third number between the first two?
(define (between? b e n)
(and (> n b) (< n e)))
 
;; A Three-numbers is (cons Number (cons Number (cons Number empty)))
 
;;three-between? : Number Number Three-numbers -> Boolean
; -- are all three numbers in the list between the first two?
(define (three-between? b e l)
(and (between? b e (first l))
(between? b e (first (rest l)))
(between? b e (first (rest (rest l))))))
 
#| Tests |#
 
(between? 0 10 2)
"should be" true
(between? 0 10 0)
"should be" false
 
(three-between? 0 10 (cons 1 (cons 2 (cons 3 empty))))
"should be" true
(three-between? 0 10 (cons 1 (cons 10 (cons 3 empty))))
"should be" false
 


Problem 2 (Solution):
;;or-list : List-of-boolean -> boolean
;; return or of list
(define (or-list a-lob)
(cond
[(empty? a-lob) false]
[else (or (first a-lob) (or-list (rest a-lob)))]))
 
 
#| Tests |#
(or-list empty)
"should be" false
(or-list (cons true (cons false empty)))
"should be" true
(or-list (cons false (cons false empty)))
"should be" false
 
 


Problem 3 (Solution):
;f : List-of-number -> Number
; -- return the difference of the number of pos and neg numbers in the list
(define (f a-lon)
(cond
[(empty? a-lon) 0]
[(cons? a-lon)
(cond
[(> (first a-lon) 0) (+ 1 (f (rest a-lon)))]
[(< (first a-lon) 0) (- (f (rest a-lon)) 1)]
[else (f (rest a-lon))])]))
 
#| Tests |#
 
(f empty)
"should be" 0
(f (cons 1 empty))
"should be" 1
(f (cons -1 (cons 1 (cons -2 empty))))
"should be" -1
 


Problem 4 (Solution):
;;string-append-n* : Non-empty-list-of-string -> String ;; -- append the
strings in the non-empty list together (define (string-append-n* a-nelostr)
(cond
[(empty? (rest a-nelostr)) (first a-nelostr)]
[(cons? (rest a-nelostr))
(string-append (first a-nelostr) (string-append-n* (rest a-nelostr)))]))
 
;;string-append* : List-of-string -> String
;; - append the strings in the list together
(define (string-append* lostr)
(cond
[(empty? lostr) ""]
[(cons? lostr)
(string-append (first lostr) (string-append* (rest lostr)))]))
 
(string-append-n* (cons "test" empty))
"should be" "test"
(string-append-n* (cons "test" (cons "ing" empty)))
"should be" "testing"
 
(string-append* empty)
"should be" ""
(string-append* (cons "test" empty))
"should be" "test"
(string-append* (cons "test" (cons "ing" empty)))
"should be" "testing"
 
 


Problem 5 (Solution):
;;how-many-symbols: Symbol List-of-symbol -> Number
;; return the number of the times that s appears in the list
(define (how-many-symbols s a-los)
(cond
[(empty? a-los) 0]
[(cons? a-los)
(cond
[(symbol=? (first a-los) s)
(+ 1 (how-many-symbols s (rest a-los)))]
[else (how-many-symbols s (rest a-los))])]))
 
;;order: Symbol Number List-of-symbol -> Number
;; return the number of a particular toy that need to be ordered
(define (order s n a-los)
(cond
[(<= n (how-many-symbols s a-los)) 0]
[else (- n (how-many-symbols s a-los))]))
 
(how-many-symbols 'doll empty)
"should be" 0
(how-many-symbols 'doll (cons 'doll (cons 'robot (cons 'doll empty))))
"should be" 2
(how-many-symbols 'doll (cons 'robot (cons 'house empty)))
"should be" 0
 
(order 'doll 10 empty)
"should be" 10
(order 'doll 3 (cons 'doll (cons 'robot (cons 'doll empty))))
"should be" 1
(order 'doll 3 (cons 'robot (cons 'house empty)))
"should be" 3
 
 
 
 
 



Jamie Raymond
Matthias Felleisen
 

23 september 2002