TDDC74 Programming: Abstraction and Modelling PRAM, Tutorial 3 The tutorial should cover the following topics: • Scheme data types: pairs, lists, and symbols • Visualization: box-and-pointer notation • List operations • Creating and using ADTs 1 Data types: pairs, lists, & symbols Problem 1 ------------------------------------------------------------------------------------------------------------------Diagram the following: (define foo ’(1 2)) foo −→ ??? (list foo ’bar foo) −→ ??? Problem 2 ------------------------------------------------------------------------------------------------------------------Diagram the following: (cons ’(a) ’(b)) −→ ??? (define ho ’(cons + ())) (car ho) −→ ??? (define hoho (cons list *)) (car hoho) −→ ?? (define hohoho (list ’cons - ’())) (car hohoho) −→ ??? Problem 3 ------------------------------------------------------------------------------------------------------------------Write a Scheme expression that will return the following structure: -->XX-->2 | V XX-->3 | V XX-->4 | V XX-->5 | V 6 1 Problem 4 ------------------------------------------------------------------------------------------------------------------Write a Scheme expression that will return the following structure: --->XX--->XX----->X/ | | | V V V darth saruman XX--->pookie | V voldemort Problem 5 ------------------------------------------------------------------------------------------------------------------The exponentiation algorithms in SICP section 1.2 are based on performing exponentiation means of repeated multiplication.1 In a similar way, one can perform integer multiplication means of repeated addition. (define (my-mult num1 num2) (if (= num2 0) 0 (+ num1 (my-mult num1 (- num2 1))))) This algorithm takes a number of steps that is linear in num2. Now suppose we include, with addition, operations double, which doubles an integer, and halve, which divides integer by 2. Using these, design a multiplication procedure that uses logarithmic number of steps. You can assume that num1 and num2 are positive integers. (define (double num) (* num 2)) (define (halve num) (/ num 2)) Problem 6 ------------------------------------------------------------------------------------------------------------------Given the following ADT: ;; symbol -> coin (define (make-coin upside) upside) ;; coin -> boolean (define (heads? coin) (eq? coin ’heads)) ;; coin -> boolean (define (tails? coin) (eq? coin ’tails)) ;; coin coin -> boolean (define coin-eq? eq?) Implement the procedure make-random-coin which creates a random coin: > (define *a-coin* (make-random-coin)) > (heads? *a-coin*) #f > (tails? *a-coin*) #t 1 This is a variation of SICP 1.17 2