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

Summary CPSC 110 - PSET 8 SOLUTIONS 2022

Rating
-
Sold
-
Pages
6
Uploaded on
28-08-2022
Written in
2022/2023

Solutions to CPSC 110 Pset 8 2022

Institution
Course









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

Written for

Course

Document information

Uploaded on
August 28, 2022
Number of pages
6
Written in
2022/2023
Type
Summary

Subjects

Content preview

;; 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-intermediate-reader.ss" "lang")((modname pset-08-solution) (read-
case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-
decimal #f #t none #f () #t)))
;; DO NOT PUT ANYTHING PERSONALLY IDENTIFYING BEYOND YOUR CWL IN THIS FILE.
;; YOUR CWLs WILL BE SUFFICIENT TO IDENTIFY YOU AND, IF YOU HAVE ONE, YOUR
;; PARTNER.
;;
(require spd/tags)

(@assignment psets/pset-08); Do not edit or remove this tag


(@problem 1)
;;
;; Design a function called sum-squares that consumes a list of naturals,
;; and produces the sum of squaring all of the naturals in the list.
;;
;; For example: (sum-squares (list 5 2 4)) produces 45.
;;
;; Your function definition must use built-in abstract functions.
;; For full marks it must be a composition of exactly 2 built-in
;; abstract functions.
;;

(@htdf sum-squares)
(@signature (listof Natural) -> Natural)
;; produce the sum of squaring all of the numbers in lon
(check-expect (sum-squares empty) 0)
(check-expect (sum-squares (list 5 2 4))
(+ (sqr 5) (sqr 2) (sqr 4)))

(@template fn-composition use-abstract-fn)

(define (sum-squares lon)
(foldr + 0 (map sqr lon)))


(@problem 2)
;;
;; Complete the design of the following function.
;;
;; Your function definition must use built-in abstract functions.
;; For full marks it must be a composition of exactly 2 built-in
;; abstract functions.
;;
;; NOTE: Looking up the function 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 letter 'a' contain word w
(check-expect (all-a-contain? empty "word") true)
(check-expect (all-a-contain? (list "bicycle" "car" "train" "walk")
"transportation")
true)

, (check-expect (all-a-contain? (list "assignment") "eight") false)
(check-expect (all-a-contain? (list "attend" "radio" "antenna" "listen" "study")
"ten")
true)
(check-expect (all-a-contain? (list "art" "bin" "action" "tin") "in") false)
(check-expect (all-a-contain? (list "art" "artery" "ban" "action" "tin")
"art")
false)

(@template fn-composition use-abstract-fn)

(define (all-a-contain? los w)
(local [(define (contains-word? s)
(string-contains? w s))
(define (starts-with-a? s)
(string=? "a" (string-ith s 0)))]
(andmap contains-word? (filter starts-with-a? los))))


;;
;; Before completing problems 3, 4, and 5, please familiarize
;; yourself with the provided data definition 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))

(define (fn-for-cat c)
(... (cat-name c)
(cat-color c)
(cat-age c)))

(define LOC1 (list C1 C2 C3 C4 C5 C6)) ; to save time with check-expects


(@problem 3)
;;
;; Design a function called brown-cat-names that consumes a list of cats,
;; and produces a list of the names of the cats that are brown in color.
;;
;; For example: (brown-cat-names LOC1)
;; produces: (list "Whiskers" "Meow" "Sassy")
;;
;; Your function definition must use built-in abstract functions.
;; For full marks it must be a composition of exactly 2 built-in
;; abstract functions.
;;

(@htdf brown-cat-names)
(@signature (listof Cat) -> (listof String))

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
travissmith1 UBC
Follow You need to be logged in order to follow users or courses
Sold
97
Member since
4 year
Number of followers
61
Documents
36
Last sold
1 month ago

3.6

16 reviews

5
6
4
6
3
0
2
0
1
4

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