Problem Sheet
(1). We would like to construct a database functions by using Scheme language. Following is a sample database for personnel information of the faculty member.
(define facultydb
(list (make-faculty (make-person “Masafumi” “Hashimoto”) 6410 “KE-208”)
Copyright By PowCoder代写 加微信 powcoder
(make-faculty (make-person “Hirokazu” “Watabe”) 6940 “KC-211”)
(make-faculty (make-person “Ivan” “Tanev”) 6699 “KC-223”)
(make-faculty (make-person “Shigeru” “Katagiri”) 7567 “KE-110”)
(make-faculty (make-person “Tomotaka” “Kimura”) 6294 “YE-214”)
(define (make-faculty person phone office)
(list person phone office))
(define make-person list)
This database includes following information with following order:
• First name
• Family name
• Extension number • Office location
Answer following questions:
(a) Draw a box-and-pointer diagram for the structure corresponding to following case:
(define testfaculty (make-faculty
(make-person “Hirohide” “Haga”) 6978 “KE-210”))
(b) Consider the abstract data type (ADT) named “faculty”. The constructor of this ADT is “make-faculty” shown above. Define following accessors:
(i). (phoneNumber faculty): which returns the phone number of given faculty. Sam- ple execution result is as:
(phoneNumber testFaculty) ==> 6978
(ii). (set-office! faculty): which sets the office name of given faculty.
(iii). (first-name faculty): which returns the first name of given faculty.
(c) Definetheprocedure“(find-faculty-with-number phoneNumber facultyDB)”,which finds the faculty whose phone number is identical to the argument “phoneNumber” in the “facultyDB”. The return value must be a family name.
(2). Answer following questions:
(a) Define the higher order procedure “(myFilter pred lst)” which returns the list of elements from the list ‘lst”, all of which satisfy the given predicate function*1) “pred”. Note that same procedure “filter is usually defined as a built-in procedure. Therefore we must use another name.
(b) Define the accessor “(phoneNumber number facultyDB)” which finds the faculty record with identical phone number “number” by using the procedure “filter” de- fined in (2)(a), and “facultyDB” is defined in (1).
(3). Answer the following questions:
(a) Define the function “(twice f x)” which applies function “f” twice to the argument
“x”. For example,
(twice square 2) ==> (square (square 2)) ==> 16
(define square (lambda (x) (* x x)))
(b) Define the function “(n-times n f x) which applies function “f” to argument “x” n-times. For example,
(twice f x) = (n-times 2 f x)
(c) Assume you have one function named “(add1 x)” which returns “x+1”. By using
this “add1” and “n-times”, define the function “(add x y)” which returns “x+y”.
(4). Let’s define the Association list or a-list. It is a list of lists. For example,
(define myAList ’((k1 v1) (k2 v2) (k3 v3)))
is an example of a-list where “ki” means the key and “vi” means the value (ki vi)(i = 1, 2, 3). Answer the following questions.
(a) Draw a box-and-pointer diagram of above a-list ‘myAList
(b) Define the procedure “(find-value a-list k)” which returns the value whose key
is identical to given “k”. You can assume that there is no duplicate key.
(5). Let’s introduce a new data type named “vector”. In order to realize the defensive pro- gramming, this data type adopts tagged data. The tag represents the dimension of vector and the elements of vector are represented as a list. For example,
(define v ’(3 (1 2 3)))
*1) The return value of “predicate function” is either “true” or “false” only. 3
represents 3 dimensional vector v = (1,2,3) Tagged data is explained in, for example, handout of Week 9. By using this tagged data, we would like to define following procedures:
• (vector-add vec1 vec2) which adds two vectors vec1 and vec2. If the dimension of two vectors are not identical, this procedure returns false (#f), otherwise return the result of vector addition. Of course, return vector must be a tagged data.
• (inner-product vec1 vec2) which computes the inner product of two vectors vec1 and vec2. If the dimension of two vectors are not identical, this procedure returns false (#f), otherwise return the inner product of two vectors.
Answer the following questions:
(a) Given two lists lst1 and lst2 whose number of elements are same, define the proce- dure (list-add lst1 lst2) which computes the element-wise addition of two lists. For example, (list-add ’(1 2) ’(3 4)) returns (4 6)((1+3 2+4)=(4 6)).
(b) Given two lists lst1 and lst2 whose number of elements are same, define the proce- dure (list-product lst1 lst2) which computes the inner product of two lists. For example,(list-product ’(1 2) ’(3 4))returns11(1×3+2×4=3+8=11).
(c) By using above two procedures list-add and list-product, define two procedures vector-add and inner-product.
Answer Sheet
Student ID ( ) Name ( ) (1).
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com