Uploaded by Emely Seheon

scheme midterm

advertisement
CS 357: Declarative Programming
Exam 2 (Spring ’21)
Academic Honesty Pledge
• I promise that I will not use an internet search engine to assist me in completing this exam.
• I promise that I will not disclose the content of this exam to other students in the class.
• I promise that I will not work collaboratively on this exam with other students in the class.
• I promise that the answers I submit to the exam questions are my own and solely the product
of my individual effort.
• I understand that I am permitted to use a Scheme interpreter or compiler to complete this
exam.
• I understand that I am permitted to use the online CS 357 class course notes and the course’s
textbook to complete this exam.
• I understand that violation of any of these promises will result in a formal university inquiry
for academic dishonesty.
Variadic Functions
The function number is variadic. It returns the number of arguments it is applied to. For example,
> (number)
0
> (number ’a)
1
> (number ’f ’o ’o)
3
Give a definition of number.
Currying
This function is only interesting because it takes four arguments:
(define sum-of-squares
(lambda (a b c d)
(+ (* a a) (* b b) (* c c) (* d d))))
It can be used like this:
> (sum-of-squares 1 2 3 4)
30
The function sum-of-squares-c is the curried version of sum-of-squares. For example,
> ((((sum-of-squares-c 1) 2) 3) 4)
30
The function curry4 curries functions of four arguments. For example,
> ((((curry4 sum-of-squares) 1) 2) 3) 4)
30
• Give a definition of sum-of-squares-c.
• Give a definition of curry4.
Using Fold
The function fold can be defined as follows:
(define fold
(lambda (proc seed)
(letrec
((pattern
(lambda (ls)
(if (null? ls)
seed
(proc (car ls)
(pattern (cdr ls)))))))
pattern)))
The function snoc can be used to add something to the end of a list. For example
> (snoc ’(1 2 3) 0)
(0 1 2 3)
> (snoc ’(a b c) ’(d))
(a b c (d))
The function take-while takes a boolean function, pred, and a list, ls, as arguments. It returns the
prefix of the list for which pred is not # f . For example
> (take-while even? ’(2 4 6 7 8 9))
(2 4 6)
> (take-while number? ’(0 1 2 a b c 3 4 5))
(0 1 2)
Demonstrate your understanding of fold by using it to
1. define snoc.
2. define take-while.
Procedural Abstraction
Consider the following five functions:
(define add
(lambda (sexpr)
(cond ((null? sexpr) 0)
((pair? sexpr)
(+ (add (car sexpr))
(add (cdr sexpr))))
(else sexpr))))
(define copy
(lambda (sexpr)
(cond ((null? sexpr) ’())
((pair? sexpr)
(cons (copy (car sexpr))
(copy (cdr sexpr))))
(else sexpr))))
(define size
(lambda (sexpr)
(cond ((null? sexpr) 0)
((pair? sexpr)
(+ (size (car sexpr))
(size (cdr sexpr))))
(else 1))))
(define depth
(lambda (sexpr)
(cond ((null? sexpr) 0)
((pair? sexpr)
(add1 (max (depth (car sexpr))
(depth (cdr sexpr)))))
(else 0))))
(define count
(lambda (pred sexpr)
(cond ((null? sexpr) 0)
((pair? sexpr)
(+ (count pred (car sexpr))
(count pred (cdr sexpr))))
((pred sexpr) 1)
(else 0))))
These functions can be used as follows:
> (add ’((1 . 2) . (3 . 4)))
10
> (copy ’(a b c d))
(a b c d)
> (size ’(a b c d))
4
> (depth ’((1 . 2) . (3 . 4)))
2
> (count even? ’((1 . 2) . (3 . 4)))
2
Demonstrate your mastery of procedural abstraction by defining a function fold-tree which abstracts the pattern of the five functions. When you are finished, use fold-tree to
• define depth.
• define count.
Hint: The function count looks like it doesn’t fit the pattern because it has an extra case and takes
an extra argument. However, you can 1) rewrite count using a helper-function to combine the last
two cases; and 2) curry count and use count-c to implement count. These two tricks will make it
look like the others.
Using the Abstraction
The function fold-tree can be used to define new functions. For example,
> (flatten-tree ’((1 . 2) . (3 . 4)))
(1 2 3 4)
> (flatten-tree ((a) ((b)) (((c)))))
(a b c)
> (map-tree add1 ’((1 . 2) . (3 . 4)))
((2 . 3) . (4 . 5))
> (map-tree even ’((1 . 2) . (3 . 4)))
((#f . #t) . (#f . #t))
Demonstrate your mastery of the fold-tree abstraction by using it to
• define flatten-tree.
• define map-tree.
Download