You are on page 1of 6

(

(
!

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.!

1. (define (find-files name directory)


(local [(define result '())
(define (recurse directory)
(begin (for-each (λ (file)
(when (string-contains? name (path->string file))
(set! result (cons file result))))
(directory-files directory))
(for-each recurse (directory-subdirectories directory))))]
(recurse directory)))
(find-files ".jpg" "/users/ian/Documents/photos")

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))

Desired output: the largest element of the list, i.e. 3.


Error: first: expects a non-empty list; given: '()

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.!
!
(
(
(
(
(
(

5. (define (power base exponent)


(if (= exponent 0)
1
(* base
(power base
(+ exponent 1)))))
(power 2 3)

Desired output: 2 raised to the 3rd power, i.e. 8.


Error: The program runs out of memory.
6. (define (my-length x)
(+ 1 (my-length (rest x))))
(my-length '(a b c))

Expected output: 3
Actual output: Exception. Attempt to take the rest of the empty list.

7. (define (apply-to-sublists procedure list-of-lists)


(map apply procedure list-of-lists))
(apply-to-sublists + '((1 2 3) (4 5 6) (7 8 9)))

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.

8. (define (iterative-fold proc start list)


(local [(define (loop in out)
(if (empty? in)
out
(loop (rest in) out)))]
(loop list start)))
(iterative-fold + 0 '(1 2 3))

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)

Expected output: a set of concentric boxes


Actual output: Exception. Program ran out of memory

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.!
!

You might also like