100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

CPSC110 problem set 8 solution

Rating
-
Sold
2
Pages
9
Uploaded on
05-01-2024
Written in
2023/2024

complete solution for problem set 8

Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Study
Course

Document information

Uploaded on
January 5, 2024
Number of pages
9
Written in
2023/2024
Type
Other
Person
Unknown

Subjects

Content preview

(@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))
$8.48
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
skye3

Get to know the seller

Seller avatar
skye3 UBC
Follow You need to be logged in order to follow users or courses
Sold
5
Member since
1 year
Number of followers
1
Documents
9
Last sold
1 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions