Homework Assignment 6

Recursion and Language Processing


CS 3540
Programming Languages and Paradigms
Spring Semester 2018


Due: Tuesday, February 27 at 8:00 AM


Introduction

This assignment asks you to write even more recursive functions in Racket. The goals of this assignment are to gain more experience with common recursion patterns and to begin applying them to programming language topics. For this assignment:

Template Source File
Download the zipped directory that contains templates for your solutions and two files of helper functions. Please use the given names as the names of the files you submit, and don't modify the provide or require clauses in any of the files.
Do Not Use...
Organizing Code



Problems

  1. Write a structurally recursive function named (tails lst) that takes a list as an argument and returns all the sublists of the list. For example:
         > (tails '(1 2 3))
         '((1 2 3) (2 3) (3) ())
         > (tails '((a b) (c d)))
         '(((a b) (c d)) ((c d)) ())
    
    Think carefully about what the function should return if it receives the empty list as an argument.

  2. Write a mutually recursive function named (n-list? obj) that takes one argument, which can be any Racket value. n-list? returns true if and only if obj is an n-list. For example:
         > (n-list? '(a b c))                ; these aren't numbers
         #f
         > (n-list? '(1 (2 (3 4) 5) 6))
         #t
         > (n-list? '(1 (2 (3 a) 5) 6))      ; oops, there's an 'a
         #f
    
    n-list? should be mutually recursive with the function num-expr?, which returns true if its argument is a number expression and false if not.

  3. Write a structurally recursive function named (tree-min bin-tree) that takes as an argument a binary tree bin-tree of this form:
         <binary-tree> ::= <number>
                         |  (<number> <binary-tree> <binary-tree>)
    
    tree-min returns the smallest value in the tree. For example:
         > (tree-min '(8 (13 11 (5 24 6)) (15 (12 10 14) 20)))
         5
    
    You will find Racket's primitive function min useful here. Note: You do not need an accumulator variable for this problem!

  4. Write a structurally recursive function named (declared-vars exp) that takes as input an expression in the little language from class:
         <exp> ::= <varref>
                 | (lambda (<var>) <exp>)
                 | (<exp> <exp>)
    
    declared-vars returns a list of all the variables declared in exp. Recall that only lambda expressions can create a variable. For example:
         > (declared-vars 'x)
         ()
         > (declared-vars '(lambda (y) (x y)))
         (y)
         > (declared-vars '(square x))
         ()
         > (declared-vars '((lambda (x)
                              (lambda (z) z))
                            (lambda (x) y) ))
         (x z x)                           ;; duplicates allowed
    
    Use the syntax procedures for the little language, as discussed in class:
         (provide exp?
                  varref?
                  lambda?   lambda->param   lambda->body
                  app?      app->proc       app->arg)
    
    You will want to use Racket's primitive function append in your solution. It combines two lists into one.


Deliverables

By the due time and date, submit the following files:

Be sure that your submission follows the submission requirements.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 18, 2018