Docsity

6 Problems with Solution - Functional Programming - Examination 1 | COSC 3015, Exams of Computer Science

Toggle sidebar
bg1
COSC 3015
Practice MidTerm Examination
1. (15 pts) Given the function defintions below, use block structure to hide the
definitions of y and sum in ss. Remove all parameters that, because of lexical
scoping, no longer need to be passed.
(define (y k a f h) (f (+ a (* h k))))
(define (sum k result h a f)
(if (< k 1)
result
(sum (- k 1) (+ result (* (if (even? k) 2 4) (y k a f h))))))
(define (ss f a b n)
(let ((h (/ (- b a) n)))
(* (/ h 3.0) (+ (y 0 a f h)(sum n 0 h a f)(y n a f h)))))
2. (20 pts) Given the function
(define (find item L test)
(cond ((null? L) nil)
((test item (car L)) L)
(else (find item (cdr L) test))))
Use the substitution model and applicative-order evaluation to determine the value of
the following expression. Show every substitution step.
(find ‘a ‘((e f t) (r a x) (e r)) memq)
pf3

Partial preview of the text

Download 6 Problems with Solution - Functional Programming - Examination 1 | COSC 3015 and more Exams Computer Science in PDF only on Docsity!

COSC 3015

Practice MidTerm Examination

  1. (15 pts) Given the function defintions below, use block structure to hide the definitions of y and sum in ss. Remove all parameters that, because of lexical scoping, no longer need to be passed. (define (y k a f h) (f (+ a (* h k)))) (define (sum k result h a f) (if (< k 1) result (sum (- k 1) (+ result (* (if (even? k) 2 4) (y k a f h)))))) (define (ss f a b n) (let ((h (/ (- b a) n))) (* (/ h 3.0) (+ (y 0 a f h)(sum n 0 h a f)(y n a f h)))))
  2. (20 pts) Given the function (define (find item L test) (cond ((null? L) nil) ((test item (car L)) L) (else (find item (cdr L) test)))) Use the substitution model and applicative-order evaluation to determine the value of the following expression. Show every substitution step. (find ‘a ‘((e f t) (r a x) (e r)) memq)
  1. (15 pts) Write a function make-min that, given two functions from numbers to numbers, returns a function that computes the minumum of the two given functions on an input argument. Show in one expression an example of a call to make-min and a call to a function created by make-min.
  2. (15 pts) Rewrite the following function to be iterative recursive: (define (length L) (if (null? L) 0 (+ 1 (length (cdr L)))))
  3. (15 pts) Draw box-and –pointer diagrams for the following: a. (1) b. (1 2) c. ((1 2) 3) d. (1 (2 3). 4) e. (cons ‘(a) ‘(b)) f. (list ‘(a) ‘(b))