The Design Recipe and BSL Expressions · Find the bug · 3 marks
A parcel up to 1 kg costs 5 dollars, a parcel over 1 kg and up to 5 kg costs 8, and anything heavier costs 12. One of the check-expects below fails.
;; Number -> Number
;; produce the postage in dollars for a parcel of w kilograms
(check-expect (postage 1) 5)
(check-expect (postage 4) 8)
(check-expect (postage 9) 12)
(define (postage w)
(cond [(<= w 5) 8]
[(<= w 1) 5]
[else 12]))Which check-expect fails, and what does the call produce? [1]
Explain the bug and write the corrected function. [2]
(postage 1) fails: it produces 8 instead of 5.
The tests of a cond are tried from top to bottom. Every weight up to 1 is also up to 5, so the first clause answers for it and the second clause can never be reached. The narrower test has to come first.
(define (postage w)
(cond [(<= w 1) 5]
[(<= w 5) 8]
[else 12]))Compound Data and Structures · Predict the output · 2 marks
Write the value of each expression, as the Interactions area would print it. If evaluation signals an error, write ERROR and say why. The definitions are in effect for all six.
(define-struct pet (name age))
(define p (make-pet "Rex" (+ 1 2)))
p
(pet-age p)
(pet-name p)
(pet? p)
(pet-age (pet-name p))
(make-pet "Max")(make-pet "Rex" 3)
3
"Rex"
#true
ERROR
ERRORThe fifth expression applies pet-age to the string "Rex", not to a pet. The last one calls make-pet with one argument, but it needs two.
Natural Numbers and Lists of Structures · Write code · 3 marks
Design the function total-price, which produces the total cost of all the lines in a list, where the cost of a line is its price times its quantity. Give the signature, the purpose, two check-expects and the function body.
(define-struct line (item price qty))
;; A Line is (make-line String Number Natural)
;; A ListOfLine is one of:
;; - empty
;; - (cons Line ListOfLine);; ListOfLine -> Number
;; produce the total cost of the lines in lol (price times quantity)
(check-expect (total-price empty) 0)
(check-expect (total-price (list (make-line "pen" 3 4)
(make-line "ink" 5 2)))
22)
(define (total-price lol)
(cond [(empty? lol) 0]
[else (+ (* (line-price (first lol)) (line-qty (first lol)))
(total-price (rest lol)))]))Mutual Recursion and Trees · Complete the code · 3 marks
The pair of functions below should tell whether a directory with a given name occurs anywhere in a tree of directories. Fill in the three blanks. Do not change any other code.
;; Dir String -> Boolean
;; produce true if d, or a directory below it, is called name
(define (has-name? d name)
(or ______________________________
(has-name?--lod (dir-subs d) name)))
;; ListOfDir String -> Boolean
;; produce true if some directory in lod, or below it, is called name
(define (has-name?--lod lod name)
(cond [(empty? lod) _______]
[else (or ______________________________
(has-name?--lod (rest lod) name))]))The blanks are (string=? (dir-name d) name), false and (has-name? (first lod) name). The empty list has no directory with the name, and false is the identity of or.
Generative Recursion, Search and Graphs · Complete the code · 3 marks
Fill in the four blanks so that quick-sort produces its argument sorted in ascending order and keeps items that are equal to the pivot.
;; (listof Number) -> (listof Number)
;; produce lon sorted in ascending order
(define (quick-sort lon)
(cond [(empty? lon) __(1)__]
[else
(local [(define pivot __(2)__)
(define (small? n) (< n pivot))
(define (big? n) (__(3)__ n pivot))]
(append (quick-sort (filter small? (rest lon)))
(list pivot)
(quick-sort (filter big? __(4)__))))]))(1) empty; (2) (first lon); (3) >=, so that items equal to the pivot are kept; (4) (rest lon), so that the pivot itself is not in the larger sublist and the problem gets smaller.
Computation, Programs, and Programming · University of British Columbia (UBC, UBC Vancouver) · also written CPSC110
Make a free, printable CPSC 110 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 450 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UBC library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CPSC 110 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of British Columbia. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.