Programming Languages and Paradigms

Spring Semester 2023

- The quiz consists of five questions. Be sure that you have all of these items and that they are all legible.
- Read all questions and their instructions thoroughly before you begin. It is always worth your time to plan ahead!
- You have also been given two blank pages.
**Use the blank sheets for your quiz answers**. Write your name on each. - The quiz is worth 60 points. Each question is worth twelve points.
- Points will be awarded based on your explicit answers. Partial credit will be given where possible, so show all of your work.
- Because the primary goal of this portion of the course is to
demonstrate the ability to write recursive programs, you
any built-in function that does the recursion for you, such as*may not use*`map`,`filter`, or`reverse`. - The quiz lasts forty (40) minutes. It is due at 1:45 PM.

- Write a structurally recursive function named
that takes as arguments a function`(tree-count test? tree)``test?`and a binary tree`tree`of this form:<binary-tree> ::= <number> | (<number> <binary-tree> <binary-tree>)

`tree-count`returns the number of values in the tree that satisfy`test?`, a one-argument function. For example:> (tree-count positive? '(1 (2 3 4) 5)) ; all five are positive 5 > (tree-count (lambda (n) (> n 10)) ; nodes bigger than 10 '(8 (13 11 (5 24 6)) (15 (12 10 14) 20))) 7

Note:`tree-count`**does**an extra parameter to solve this problem! Use regular structural recursion.*not*need - Write a mutually recursive procedure
, which takes a prefix binary expression as an argument.`(annotate exp)`<prefix-exp> ::= (<operator> <number-exp> <number-exp>) <number-exp> ::= <number> | <prefix-exp>

`annotate`returns an expression of the same form, with the operator`op`replaced by the pair`(op . level)`.`level`records the 0-based depth of the operation in the tree. For example:> (annotate '(+ 4 5)) '((+ . 0) 4 5) > (annotate '(* (+ 4 5) (/ 7 6))) '((* . 0) ((+ . 1) 4 5) ((/ . 1) 7 6)) > (annotate '(* (+ 4 5) (/ 7 (+ 6 5)))) '((* . 0) ((+ . 1) 4 5) ((/ . 1) 7 ((+ . 2) 6 5)))

Makean interface procedure that calls a structurally recursive helper function with two arguments: the expression and an initial value for the level counter.`annotate`

must be mutually recursive with the function`annotate`, which annotates number expressions.`(annotate-ne num-exp level)` - Answer
**very briefly**the following questions. One sentence or Racket expression is sufficient for each.- How does
**program derivation**improve the efficiency of a mutually-recursive solution? - This version of the
`factorial`function is**tail recursive**:(define factorial (lambda (n acc) (if (zero? n) acc (factorial (sub1 n) (* n acc)))))

What does that statement mean, in terms of*the way the function executes at run time*? - A number can be defined inductively as a sequence of
digits:
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <number> ::= <digit> | (10 * <number>) + <digit>

Write the framework of the`if`(or`cond`) expression that will be the main control structure of any structurally-recursive function that processes numbers of this form. Use comments for the 'then' and 'else' clauses to show the cases they handle. - The expression
(occurs-free?

is*x**exp*)**not**always equal to the expression(not (occurs-bound?

for a given*x**exp*))`x`and`exp`.

Give an example that illustrates why.

*Extra Credit*. There are two ways in which this is true. Give**distinct**examples that illustrate both ways.

- How does
- Write a mutually-recursive function
that takes two arguments, a symbol`(slist-contains? s slst)``s`and an s-list`slst`:<s-list> ::= () | (<symbol-expression> . <s-list>) <symbol-expression> ::= <symbol> | <s-list>

returns true if`slist-contains?``s`appears anywhere in`slst`, and false otherwise. For example:> (slist-contains? 'eugene '((a b) (((b g r) (f r)) c (d e)) b)) #f > (slist-contains? '/ '(if (zero? n) zero (/ total n))) #t

must be mutually recursive with the function`slist-contains?`, which determines if a symbol appears anywhere in a`symexp-contains?`*symbol expression*. - Write a structurally recursive function
that takes two arguments: a symbol`(contains-varref? v exp)``v`and an expression`exp`in our little language:<exp> ::= <varref> | (lambda (<var>) <exp>) | (<exp> <exp>)

returns true if`contains-varref?``v`is used as a variable reference anywhere in`exp`, and false otherwise. For example:> (contains-varref? 'y 'y) #t > (contains-varref? 'x '(lambda (x) x)) #t > (contains-varref? 'y '(lambda (y) x)) #f > (contains-varref? 'x '(f (lambda (y) x))) #t > (contains-varref? 'x '( (lambda (x) y) ; x is not used here (lambda (y) x) )) ; but x is used here #t

You must use these**syntax procedures**for the little language, as provided in class.exp? varref? lambda? lambda->param lambda->body app? app->proc app->arg