Professional Documents
Culture Documents
(
!
Part 2
Each of the following questions shows some code being executed at the Racket prompt,
along with the output or error it generated, and the intended output that the programmer
wanted. Give the correction to the code to produce the desired result.
• Fix!the!code!that’s!there;!don’t(rewrite(it(from(scratch.!!In!grading,!we’re!looking!
for!evidence!that!you!understand!the!bug!in!that!particular!code,!not!that!you!
understand!how!to!write!new!code.!
• You!do!not!need!to!provide!an!explanation,!although!you!are!free!to!do!so!if!you!
like.!
• It!is!sufficient!to!write(your(correction(on(top(of(the(existing(code;!you!don’t(
need(to(recopy!it.!
Desired output: a list of all the files in directory whose pathnames contain name. Note:
assume string-contains? returns true if and only if the string file contains the string name.
Error: None. It runs fine, but it doesn’t return any value at all.
2. (define!(iterated,overlay!proc!count)!
!!(local![(define!result!empty,image)!
!!!!!!!!!!!!!!(define!(loop!n)!
!!!!!!!!!!!!!!!!!(when!(<!n!0)!
!!!!!!!!!!!!!!!!!!!(begin!(set!!result!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(overlay!(proc!n)!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!result))!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(loop!(,!n!1)))))]!
!!!!!!!!!(begin!(loop!(,!count!1))!
!!!!!!!!!!!!!!!!!!!!!result)))!
(iterated,overlay!(λ!(n)!(square!(*!n!10)!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"outline"!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"black"))!
!!!!!!!!!!!!!!!!!!10)!
!
Desired!output!
!
Error:!None,!but!it!always!returns!a!blank!picture.!
!
(
3. (define (max-list list)
(local [(define max (first list))
(define remaining list)
(define (loop)
(begin (when (> (first remaining)
max)
(set! max (first remaining)))
(set! remaining (rest remaining))
(loop)))]
(begin (loop)
max)))
(max-list (list 1 2 3))
4. (define!(sum,odd!list)!
!!(local![(define!sum!0)]!
!!!!!(begin!(for,each!(λ!(e)!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(when!(odd?!e)!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(+!sum!e)))!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!list)!
!!!!!!!!!!!!!!!!!sum)))!
(sum,odd!(list!1!2!3))!
!
Desired!output:!the!sum!of!all!the!odd!numbers!in!the!list!
Error:!none,!but!output!is!always!0.!
!
(
(
(
(
(
(
Expected output: 3
Actual output: Exception. Attempt to take the rest of the empty list.
The intention is that apply-to-lists takes a list of lists, and calls procedure
on each of the sublists, then returns a list of all the results.
Expected output: (6 15 24), that is the sum of (1 2 3), followed by the
sums of (4 5 6) and (7 8 9).
Actual output: Exception in call to map.
Expected output: 6
Actual output: 0
9. (define (iterated-overlay proc count)
(if (= count 0)
empty-image
(overlay (proc count)
(iterated-overlay proc count))))
(iterated-overlay (λ (n) (square n “outline” “black”)) 10)
10. (define,struct!animal!(name))!
(define,struct!(mammal!animal)!()!
!!!#:methods!
!!!(define!(legs!m)!4)!
!!!(define!(carnivorous?!m)!
!!!!!!!false))!
(define,struct!(cat!mammal)!()!
!!!!#:methods!
!!!!(define!(carnivorous?!c)!
!!!!!!!true)!
!!!!(define!(likes,to,sleep,on,radiators?!c)!
!!!!!!!true))!
(define,struct!(human!mammal)!()!
!!!!#:methods!
!!!(define!(legs!h)!2)!
!!!(define!(carnivorous?!h)!
!!!!!!!true))!
(define!lady=gaga!(make,human!"Lady!Gaga"))!
(likes,to,sleep,on,radiators?!lady,gaga)!
!
Expected(value:!false!
Error:(method:!Expected(a(structure(that(defines(the(method(`likes6to6sleep6on6
radiators?`(but(was(given(#(struct:human("Lady(Gaga")(
!
(
(
(
!
(
(
(
(
!
11. (define,struct!color!(red!green!blue))!
(define,struct!(transparent=color!color)!(alpha))!
(define!c!(make,transparent,color!255!0!0!128))!
(transparent,color,red!c)!
!
Expected(value:!255!
Error:!transparent,color,red:!this!function!is!not!defined.!
!