(@problem 1)
;;
;; Complete the design of a func:on called sum-squares that consumes a list
;; of natural numbers and produces the sum of squaring all of the naturals
;; in the list.
;;
;; For example: (sum-squares (list 5 2 4)) produces 45.
;;
;; Your func:on defini:on must use built-in abstract func:ons.
;; For full marks it must be a composi:on of exactly 2 built-in
;; abstract func:ons.
;;
(@htdf sum-squares)
(@signature (listof Natural) -> Natural)
;; produce sum of squaring all of naturals in lon
(check-expect (sum-squares empty) 0)
(check-expect (sum-squares (list 1 2 3))
(+ (sqr 1) (sqr 2) (sqr 3)))
;(define (sum-squares lon) empty)
(@template-origin fn-composi:on
use-abstract-fn)
(define (sum-squares lon)
(foldr + 0 (map sqr lon)))
(@problem 2)
;;
;; Complete the design of the following func:on.
;;
;; Your func:on defini:on must use built-in abstract func:ons.
;; For full marks it must be a composi:on of exactly 2 built-in
;; abstract func:ons.
;;
;; NOTE: Looking up the func:on string-contains? in the help desk
;; might be helpful...
(@htdf all-a-contain?)
(@signature (listof String) String -> Boolean)
;; determine if all strings that begin with the leUer 'a' contain word w
(check-expect (all-a-contain? empty "word") true)
, (check-expect (all-a-contain? (list "bicycle" "car" "train" "walk")
"transporta:on")
true)
(check-expect (all-a-contain? (list "assignment") "eight") false)
(check-expect (all-a-contain? (list "aUend" "radio" "antenna" "listen" "study")
"ten")
true)
(check-expect (all-a-contain? (list "art" "bin" "ac:on" ":n") "in") false)
(check-expect (all-a-contain? (list "art" "artery" "ban" "ac:on" ":n")
"art")
false)
;(define (all-a-contain? los w) false) ;stub
(@template-origin fn-composi:on
use-abstract-fn)
(define (all-a-contain? los w)
(local [(define (contain-word? s)
(string-contains? w s))
(define (start-with-a? s)
(string=? "a" (string-ith s 0)))]
(andmap contain-word? (filter start-with-a? los))))
(@problem 3)
;;
;; Before comple:ng problems 3, 4, and 5, please familiarize
;; yourself with the provided data defini:on for a Cat.
;; An example (listof Cat) is also provided.
;;
(@htdd Cat)
(define-struct cat (name color age))
;; Cat is (make-cat String Color Natural)
;; interp. a cat with a name, coat color, and age (in years)
(define C1 (make-cat "Whiskers" "brown" 13))
(define C2 (make-cat "Si" "black" 4))
(define C3 (make-cat "Am" "white" 4))
(define C4 (make-cat "Meow" "brown" 7))
(define C5 (make-cat "Garfield" "orange" 8))
(define C6 (make-cat "Sassy" "brown" 6))
;;
;; Complete the design of a func:on called sum-squares that consumes a list
;; of natural numbers and produces the sum of squaring all of the naturals
;; in the list.
;;
;; For example: (sum-squares (list 5 2 4)) produces 45.
;;
;; Your func:on defini:on must use built-in abstract func:ons.
;; For full marks it must be a composi:on of exactly 2 built-in
;; abstract func:ons.
;;
(@htdf sum-squares)
(@signature (listof Natural) -> Natural)
;; produce sum of squaring all of naturals in lon
(check-expect (sum-squares empty) 0)
(check-expect (sum-squares (list 1 2 3))
(+ (sqr 1) (sqr 2) (sqr 3)))
;(define (sum-squares lon) empty)
(@template-origin fn-composi:on
use-abstract-fn)
(define (sum-squares lon)
(foldr + 0 (map sqr lon)))
(@problem 2)
;;
;; Complete the design of the following func:on.
;;
;; Your func:on defini:on must use built-in abstract func:ons.
;; For full marks it must be a composi:on of exactly 2 built-in
;; abstract func:ons.
;;
;; NOTE: Looking up the func:on string-contains? in the help desk
;; might be helpful...
(@htdf all-a-contain?)
(@signature (listof String) String -> Boolean)
;; determine if all strings that begin with the leUer 'a' contain word w
(check-expect (all-a-contain? empty "word") true)
, (check-expect (all-a-contain? (list "bicycle" "car" "train" "walk")
"transporta:on")
true)
(check-expect (all-a-contain? (list "assignment") "eight") false)
(check-expect (all-a-contain? (list "aUend" "radio" "antenna" "listen" "study")
"ten")
true)
(check-expect (all-a-contain? (list "art" "bin" "ac:on" ":n") "in") false)
(check-expect (all-a-contain? (list "art" "artery" "ban" "ac:on" ":n")
"art")
false)
;(define (all-a-contain? los w) false) ;stub
(@template-origin fn-composi:on
use-abstract-fn)
(define (all-a-contain? los w)
(local [(define (contain-word? s)
(string-contains? w s))
(define (start-with-a? s)
(string=? "a" (string-ith s 0)))]
(andmap contain-word? (filter start-with-a? los))))
(@problem 3)
;;
;; Before comple:ng problems 3, 4, and 5, please familiarize
;; yourself with the provided data defini:on for a Cat.
;; An example (listof Cat) is also provided.
;;
(@htdd Cat)
(define-struct cat (name color age))
;; Cat is (make-cat String Color Natural)
;; interp. a cat with a name, coat color, and age (in years)
(define C1 (make-cat "Whiskers" "brown" 13))
(define C2 (make-cat "Si" "black" 4))
(define C3 (make-cat "Am" "white" 4))
(define C4 (make-cat "Meow" "brown" 7))
(define C5 (make-cat "Garfield" "orange" 8))
(define C6 (make-cat "Sassy" "brown" 6))