;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname l08-list-functions) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor mixed-fraction #f #t none #f () #t)))
;; A (listof X) is one of:
;; * empty
;; * (cons X (listof X))
;; listof-X-template: (listof X) -> Any
(define (listof-X-template lox)
(cond [(empty? lox) ...]
[(cons? lox) (... (X-template (first lox))
(listof-X-template (rest lox)))]))
(define wishlist (cons "bicycle"
(cons "video game"
(cons "play-doh"
(cons "turtle"
(cons "comic book"
empty))))))
;; (count-wishes los) counts the number of wish in los
;; Examples:
(check-expect (count empty) 0)
(check-expect (count wishlist) 5)
;; count-wishes: (listof Any) -> Num
(define (count los)
(cond [(empty? los) 0] ; <- basecase
[(cons? los) (+ 1 ; <- recursive case
(count (rest los)))]))
;; (count-bicycles los) counts the number of bycicles in wish-list
;; Examples:
(check-expect (count-bicycles empty) 0)
(check-expect (count-bicycles wishlist) 1)
;; count-bicycles: (listof Str) -> Num
(define (count-bicycles wish-list)
(cond [(empty? wish-list) 0] ; <- basecase
[(string=? (first wish-list) "bicycle") (+ 1 (count-bicycles (rest wish-list)))]
[else (count-bicycles (rest wish-list))]))
;; (count-string s los) counts the number of s in los
;; Examples:
(check-expect (count-string "bicycle" empty) 0)
(check-expect (count-string "bicycle" wishlist) 1)
;; count-string: (listof Str) -> Num
(define (count-string s los)
(cond [(empty? los) 0] ; <- basecase
[(string=? (first los) s) (+ 1 (count-string s (rest los)))]
[else (count-string s (rest los))]))
;; (negate-list lon) consumes a list of numbers and produces a
;; list of all the numbers multiplied by -1
;; Examples:
(check-expect (negate-list empty) empty)
(check-expect (negate-list (cons 1 (cons 2 empty))) (cons -1 (cons -2 empty)))
;; negate-list: (listof Num) -> (listof Num)
(define (negate-list lon)
(cond [(empty? lon) empty]
[(cons? lon) (cons (* -1 (first lon))
(negate-list (rest lon)))]))
;; (ne-listof X) is one of:
;; * (cons X empty)
;; * (cons X (ne-listof X))
;; (count-wishes ne-los) counts the number of strings in los
;; Examples:
(check-expect (count-ne (cons "a" empty)) 1)
(check-expect (count-ne wishlist) 5)
;; count-wishes: (listof Any) -> Num
(define (count-ne los)
(cond [(empty? (rest los)) 1] ; <- basecase
[(cons? los) (+ 1 ; <- recursive case
(count-ne (rest los)))]))