;; 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 "2017-fall-reader.rkt" "csc104")((modname Week-10-Lab) (compthink-settings #hash((prefix-types? . #f)))) ; Exercises for Week 10 ; ===================== ; The quiz on November 22nd will be related to this exercise. ; You should do very well on the quiz if you try out everything below. ; Instructors/TAs are available before quizzes for questions, 10--3 on Wednesday in BA 3175. ; Quiz Rooms ; ---------- ; TUT0201 11-12 ; 11:30 surnames A--L RS310 ; 11:30 surnames M--V HA316 ; 11:45 surnames W--Z RS310 ; ; TUT0301 12--1 ; 12:30 surnames A--M BF323 ; 12:45 surnames N--Z BF323 ; ; TUT0401 1--2 ; 1:30 surnames A--M UC256 ; 1:45 surnames N--Z UC256 ; ; TUT0501 2--3 ; 2:30 surnames A--M RS310 ; 2:45 surnames N--Z RS310 ; ; TUT0101 3--4 ; 3:30 surnames A--M RW140 ; 3:45 surnames N--Z RW140 ; Nested List Recursion Stepping ; ; Below are various recursive functions we have asked students to understand and step ; on quizzes and exams from previous instances of the course. ; A. (define (A x) (cond [(list? x) (apply + (map A x))] [else 1])) ; Show the result value: #;(A "a") ; Show the steps: #;(step parallel [hide A] (map A (list "b" "c"))) ; Show the steps: #;(step parallel [hide (map A (list "b" "c"))] (apply + (map A (list "b" "c")))) ; Show the result value: #;(A (list "b" "c")) ; Show the steps: #;(step parallel [hide A] (map A (list "a" (list "b" "c")))) ; Show the result value: #;(A (list (list "a") (list "b" "c"))) ; B. (define (B LLL) (cond [(list? LLL) (reverse (apply append (map B LLL)))] [else (list LLL)])) ; Show the result values: #;(B 1) #;(B (list 1 2)) ; Show the non-cond steps: #;(step parallel [hide (B (list 1 2)) (B 3) (B 4)] (B (list (list 1 2) 3 4))) ; Show the result value: #;(B (list 0 (list (list 1 2) 3 4))) ; C. (define (C LLL) (cond [(list? LLL) (+ (length LLL) (apply + (map C LLL)))] [else 1])) ; Show the result values: #;(C "a") #;(C (list "b" "c" "d")) ; Show the non-cond steps: #;(step parallel [hide (C "a") (C (list "b" "c" "d"))] (C (list "a" (list "b" "c" "d")))) ; D. (define (D LLL) (cond [(number? LLL) LLL] [else (apply + (map D LLL))])) ; Show the result values: #;(D 5) #;(D (list 3 4)) ; Show the non-cond steps: #;(step parallel [hide (D 3) (D 4) (D (list 5 6))] (D (list 3 4 (list 5 6)))) ; You may omit showing steps that contain a 'cond' expression. ; E. (define (E LLL) (cond [(list? LLL) (reverse (map E LLL))] [else LLL])) ; Show the result values: #;(E 1) #;(E (list 2 3)) ; Show the non-cond steps: #;(step parallel [hide (E 1) (E (list 2 3))] (E (list 1 (list 2 3)))) ; Show the result value: #;(E (list (list 1 (list 2 3)) (list 4 5))) ; F. (define (F LLL) (cond [(list? LLL) (+ 1 (apply maximum (map F LLL)))] [else 0])) ; Show the result values: #;(F "a") #;(F (list "b" "c" "d")) ; Show the non-cond steps: #;(step parallel [hide (F "a") (F (list "b" "c" "d"))] (F (list "a" (list "b" "c" "d")))) ; Show the result value [this would be too much for a quiz]: #;(F (list (list "a" (list "b" "c")) (list "d" (list (list "e" "f") "g")))) ; G. ; Produces an image containing the text of a-string, with height 12. (define (I a-string) (string->image a-string 12 "black")) (define (G s) (cond [(string? s) (I s)] [else (beside (rectangle 50 1 "solid" "black") (apply above (map G s)))])) ; Show the result values: #;(G "rain") #;(G (list "in" "spain")) ; Show the non-cond steps: #;(step parallel [hide (G "on") (G (list "the" "plain"))] (G (list "on" (list "the" "plain")))) ; Show the result value [this would be too much for a quiz]: #;(G (list "rain" (list "in" "spain") "falls" (list "on" (list "the" "plain"))))