Homework Assignment 6

Recursion and Language Processing

CS 3540
Programming Languages and Paradigms
Spring Semester 2023

Due: Tuesday, March 4 at 10:00 AM


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 your file of Rackunit tests. 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.

With provide, you must define all five functions. If you don't have time to solve a problem, define a function that takes the correct number of arguments and returns a legal default value, such as 0 or '().

The zip file also includes two files that contain helper functions we saw in class. Keep them in the folder with your solutions and tests. You will not submit these files.

As always, please use the assigned names for the two files you do submit, homework06.rkt and homework06-tests.rkt.
Do Not Use...
Organizing Code


  1. Write a structurally recursive function (list-index s los) that takes two arguments, a symbol and a list of symbols. list-index returns the 0-based first occurrence of s in los. If s does not occur in the list, it returns -1. For example:
        > (list-index 'd '(a b c d e f d))
  2. Write a structurally recursive function named (tree-sum bin-tree) that takes as an argument a binary tree bin-tree of this form:
         <binary-tree> ::= <number>
                         |  (<number> <binary-tree> <binary-tree>)
    tree-sum returns the sum of all the values in the tree. For example:
         > (tree-sum '(1 (2 3 4) 5))
         > (tree-sum '(8 (13 11 (5 24 6)) (15 (12 10 14) 20)))
    Note: You do not need an accumulator variable for this problem! Simple structural recursion is your friend.

  3. Write a structurally recursive function named (tree? exp) that takes one argument, which can be any Racket expression. (tree? exp) returns true if exp is a bin-tree as defined for Problem 2, and false otherwise. For example:
         > (tree? '1)
         > (tree? '(8 (13 11 (5 24 6)) (15 (12 10 14) 20)))
         > (tree? 'x)                                          ; not a number
         > (tree? '(1 (2 3 4) (2 3 4 5)))                      ; too many items in a tree
         > (tree? '(8 (13 x (5 24 6)) (15 (12 10 14) 20)))     ; contains non-number
    This is your second type predicate. Follow the datatype for this problem! Simple structural recursion is your friend.

  4. Write a structurally recursive function (unused-var? v exp) that takes as input a symbol and an expression in the the little language from class:
         <exp> ::= <varref>
                 | (lambda (<var>) <exp>)
                 | (<exp> <exp>)
    unused-var? returns true if v is declared as a variable anywhere in exp and never used. Recall that only lambda expressions can declare a variable. For example:
         ; x is declared and not used
         > (unused-var? 'x '(lambda (x) y))
         ; x is declared *and* used
         > (unused-var? 'x '(lambda (y) (lambda (x) x)))
         ; ... but y is declared and not used
         > (unused-var? 'y '(lambda (y) (lambda (x) x)))
         ; here, x is declared and used in the app's argument
         > (unused-var? 'x '(a (lambda (z) (lambda (y) (lambda (x) x)))))
    Three quick notes:


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 28, 2023