Docsity

Recursive Definition, ADT Specification - Programming Languages | CS 784, Exams of Programming Languages

Toggle sidebar
bg1
Wright State University
Department of Computer Science
and Engineering
CS 784 Fall 2008 Prasad
Midterm (30 pts)
1 Recursive Definition (8 pts)
Define a function distinctSymbols that takes a nested list of numbers and
symbols as input, and returns a flat list of distinct symbols in the input. (In
other words, the length of the resulting flat list must be the count of the
distinct symbols in the input nested list.)
(distinctSymbols โ€™(1)) returns ()
(distinctSymbols โ€™(256 x (y))) returns (x y)
(distinctSymbols โ€™(((a)) b (2 (a) (c c c))))) returns (a b c)
2 Recursive Definition (4 + 2 pts)
Consider the following mystery function.
(define (mystery n)
(letrec
( (AUX
(lambda (n s) (if (zero? n) (list s)
(append (AUX (- n 1) (cons 0 s))
(AUX (- n 1) (cons 1 s))
)
)
)))
(AUX n ())
)
)
What is the value of (mystery 2)? (Hint: Trace the code bottom-up
carefully.)
Describe the value returned when mystery is invoked with a natural num-
ber. (Hint : Reason inductively.)
1
pf2

Partial preview of the text

Download Recursive Definition, ADT Specification - Programming Languages | CS 784 and more Exams Programming Languages in PDF only on Docsity!

Wright State University Department of Computer Science and Engineering

CS 784 Fall 2008 Prasad

Midterm (30 pts)

1 Recursive Definition (8 pts)

Define a function distinctSymbols that takes a nested list of numbers and symbols as input, and returns a flat list of distinct symbols in the input. (In other words, the length of the resulting flat list must be the count of the distinct symbols in the input nested list.)

(distinctSymbols โ€™(1)) returns () (distinctSymbols โ€™(256 x (y))) returns (x y) (distinctSymbols โ€™(((a)) b (2 (a) (c c c))))) returns (a b c)

2 Recursive Definition (4 + 2 pts)

Consider the following mystery function.

(define (mystery n) (letrec ( (AUX (lambda (n s) (if (zero? n) (list s) (append (AUX (- n 1) (cons 0 s)) (AUX (- n 1) (cons 1 s)) ) ) ) ) ) (AUX n ()) ) )

What is the value of (mystery 2)? (Hint: Trace the code bottom-up carefully.) Describe the value returned when mystery is invoked with a natural num- ber. (Hint: Reason inductively.)

3 ADT Specification (6 + 10 pts)

You are required to specify the ADT Int that supports the following integer operations: zero, succ, pred, negate, plus, and minus. Informally,

  • zero: Yields 0.
  • succ: Takes an integer as input, and yields the next integer (that is, successor).
  • pred: Takes an integer as input, and yields the previous integer (that is, predecessor).
  • negate: Takes an integer as input, and yields its negative (that is, additive inverse).
  • plus: Takes two integers as input, and yields their sum (that is, addi- tion).
  • minus: Takes two integers as input, and yields their difference (that is, subtraction).
  1. Specify the signatures of the aforementioned operations on ADT Int.
  2. Give an algebraic specification of the ADT Int.