Anda di halaman 1dari 2

CMPSC 461: Programming Language Concepts

Midterm 1 Practice Questions


Problem 1 λ-calculus.
a) What are the free variables in the λ-term (λx .(λy . y z) y) x).

b) Fully evaluate (λx .(λy .((λy . y) x))) z.

c) Define a function doubleChurch, which takes a Church Numeral and doubles it. Try to build it from
scratch, without using the encoding of + or ∗ as defined in lecture.

Problem 2 Higher-order functions and currying.


a) What is the curried form of λx y . (+ x y).

b) Explain in one sentence, what’s the result of (λx y . (+ x y)) 1.

Problem 3 Scheme.
a) Define a recursive Scheme function that takes a list of numbers and returns the product of all the list
elements. Given the empty list, the product should be 1.

b) Define the same functions using “fold-left”.

Problem 4 Scheme.
What do the following functions do?
(define (filter p l)
(cond ((null? l) ’())
(#t (let ((result (filter p (cdr l))))
(if (p (car l)) (cons (car l) result) result)))))
(define (pink p l) (not (null? (filter p l))))
(define (blue p l) (= (length l) (length (filter p l))))
(define (renmap f x ys) (map (lambda (y) (f x y)) ys))
(define (binmap f xs ys) (map (lambda (x) (renmap f x ys)) xs))

Problem 5 Regular Expressions and automata.


a) Define a regular expression for all strings of odd length, over the alphabet of {0}.

b) Draw a deterministic finite state automaton which accepts the same language as defined by the regular
expression above.

c) Draw a nondeterministic finite state automaton which accepts all strings that end with “ed”.

d) Define a regular expression for identifiers over the alphabet of {A,B,C,a,b,c,0,1,2,3,4,5,6,7,8,9}, such
that an identifier must begin with an alphabetic character and must contain at least one numeric character.

e) Try to modify the definition above so that identifiers still begin with an alphabetic character, but after
that, it must contain at least one numeric, at least one lower-case letter, and at least one upper-case letter,
but no restrictions on where they can/must occur.

1/2
Problem 6 Context free grammar. Consider the following context free grammar:

P ::= id | P ∨ P | P ∧ P | ¬P | (P )
id ::= p | q | r | s

where the set of terminals symbols is {p, q, r, s, ∨, ∧, ¬, (, )} and the start symbol is P .
a) Give leftmost derivations for the following expressions:

• p ∧ ¬q
• p∨q∨r
• ¬(p ∧ q)

b) Show that this language is ambiguous.

c) Define an unambiguous grammar which accepts the same language. We assume ∧ and ∨ are left-
associative, ¬ is right-associatvie and has a higher precedence than ∧ and ∨.

Problem 7 Context Free Grammar. For each of the following grammars state whether it is ambiguous or
unambiguous. If it is ambiguous give an equivalent unambiguous grammar. If it is unambiguous, state all
the precedences and associativities enforced by the grammar. Assume Id generates identifiers.

a) E ::= E + F | E − F | F
F ::= −F |Id | (E)

b) E ::= E ∨ F | F
F ::= G ∩ F | G ∪ F | G
G ::= G ∧ H | H
H ::= Id | (E)

Midterm 1 Practice Questions, Cmpsc 461 2017 Fall

Anda mungkin juga menyukai