1 Assignment 1: Recursion and Higher-Order Functional Abstractions

Recursion is the root of computation since it trades description for time.

——Alan Perlis

Guidelines for this assignment
Assignment

Write the following recursive Racket procedures. Place all of your code in a file named a1.rkt, and submit it via the autograder. Please make sure your file has exactly this filename, and that it runs, before submitting.

Please read through our Course policies before beginning the rest of the assignment.

  1.   Nest = ()
      | (Nest)
         
      List-of-Nest = ()
      | (Nest . List-of-Nest)

    Define and test a procedure unwrap that takes a Nest and returns a List-of-Nest that contains all the Nests contained in the given Nest, from big to small: starting with itself and ending with ().

    > (unwrap '())
    (())
    > (unwrap '((())))
    (((())) (()) ())
    > (unwrap '(((((()))))))
    ((((((()))))) ((((())))) (((()))) ((())) (()) ())
  2. Like unwrap, define and test a procedure countdown that takes a natural number and returns a list of the natural numbers less than or equal to that number, in descending order.

    > (countdown 5)
    (5 4 3 2 1 0)
  3.   Spine = ()
      | (Int Spine)
      | (Int Spine Int)

    Define and test a procedure mirror that takes a Spine and returns a new Spine in which each (x s y) changes to (y s x). Here x and y are Ints, and s is a Spine.

    > (mirror '(1 (20 (3 (40 (5 () 60)) 70) 8)))
    (1 (8 (70 (40 (60 () 5)) 3) 20))
  4.   Bunch = ()
      | (Int Bunch)

    Define and test a procedure removeB that takes an Int and a Bunch and returns a new Bunch with every occurrence of the Int removed.

    > (removeB 2 '(5 (2 ())))
    (5 ())
    > (removeB 2 '(5 (2 (3 (2 (6 (1 ())))))))
    (5 (3 (6 (1 ()))))
  5. A predicate is a procedure that takes a single argument and returns either #t or #f. The even? predicate, for example, returns #t if its argument is an even number and #f if its argument is a non-even number.

    Define and test a procedure repeat-1st that takes a predicate and a list and returns a new list that is like the given list but repeats the first member that satisfies the predicate. The member satisfies the predicate if the predicate returns #t for that member.

    > (repeat-1st zero? '(5 6 3 4 1))
    (5 6 3 4 1)
    > (repeat-1st even? '(5 2 6 3 4 1))
    (5 2 2 6 3 4 1)
  6. Define and test a procedure zip that takes two lists and forms a new list, each member of which is a pair formed by combining the corresponding members of the two input lists. If the two lists are of uneven length, then drop the tail of the longer one.

    > (zip '(1 2 3) '(a b c))
    ((1 . a) (2 . b) (3 . c))
    > (zip '(1 2 3 4 5 6) '(a b c))
    ((1 . a) (2 . b) (3 . c))
    > (zip '(1 2 3) '(a b c d e f))
    ((1 . a) (2 . b) (3 . c))
  7. Define and test a procedure list-index-ofv that takes a member and a list and returns the (base 0) index of that member in the list. A list missing that member will be considered bad data.

    > (list-index-ofv 'x '(x y z x x))
    0
    > (list-index-ofv 'x '(y z x x))
    2
  8. Define and test a procedure append that takes two lists, ls1 and ls2, and appends ls1 to ls2.

    > (append '(42 120) '(1 2 3))
    (42 120 1 2 3)
    > (append '(a b c) '(cat dog))
    (a b c cat dog)
  9. Define and test a procedure reverse that takes a list and returns the reverse of that list.

    > (reverse '(a 3 x))
    (x 3 a)
  10. Define and test a procedure repeat that takes a list and a natural number and returns a new list with repeating sequence of the input list. The number of times the new list repeats is equal to the natural number, which is the second argument.

    > (repeat '(4 8 11) 4)
    '(4 8 11 4 8 11 4 8 11 4 8 11)
  11.   Tree = Int
      | (Tree Tree)

    Define and test a procedure mirrored? that takes two Trees and returns #t if they are mirror images of each other or #f otherwise. The mirror image of a Tree is the result of changing (x y) to (y x) everywhere. Here x and y are Trees.

    > (mirrored? 1 1)
    #t
    > (mirrored? 1 2)
    #f
    > (mirrored? '(1 2) '(2 1))
    #t
    > (mirrored? '((1 3) 2) '(2 (3 1)))
    #t
    > (mirrored? '(1 (3 2)) '((2 3) 1))
    #t
    > (mirrored? '((1 3) 2) '((2 3) 1))
    #f
  12.   Wood = ()
      | (Int Lawn Wood)
         
      Lawn = ()
      | (Int Wood Lawn)

    Define and test a procedure total-wood that takes a Wood and returns the sum of all Ints x in the Wood (x l w). Here x is an Int, l is a Lawn, and w is a Wood. You should ignore the Ints in a Lawn. Hint: you may define a helper function that works on Lawn.

    > (total-wood '(3 () (4 () (5 () ()))))
    12
    > (total-wood '(3 () (4 (5 () ()) ())))
    7
    > (total-wood '(3 (5 () ()) (4 () ())))
    7
    > (total-wood '(3 (5 (4 () ()) ()) ()))
    7
  13. The expressions '(a b) and '(a . (b . ())) are equivalent. Using this knowledge, rewrite the expression '((w x) y (z)) using as many dots as possible. Be sure to test your solution using Racket’s equal? predicate. (You do not have to define a rewrite procedure; just rewrite the given expression by hand and place it in a comment.)

  14.   Bit = 0
      | 1
         
      List-of-Bit = ()
      | (Bit . List-of-Bit)

    Define and test a procedure binary->natural that takes a List-of-Bit representing an unsigned binary number in reverse bit order and returns that number. For example:

    > (binary->natural '())
    0
    > (binary->natural '(0 0 1))
    4
    > (binary->natural '(0 0 1 1))
    12
    > (binary->natural '(1 1 1 1))
    15
    > (binary->natural '(1 0 1 0 1))
    21
    > (binary->natural '(1 1 1 1 1 1 1 1 1 1 1 1 1))
    8191
  15. The procedure map takes a procedure p of one argument and a list ls and returns a new list containing the results of applying p to the members of ls. For example,

    > (map sub1 '(1 2 3 4))
    (0 1 2 3)

    Here is the definition of map:

    (define map
      (lambda (p ls)
        (cond
          ((null? ls) '())
          (else (cons (p (car ls)) (map p (cdr ls)))))))

    Define a procedure append-map that, similar to map, takes both a procedure p of one argument, a list of inputs ls, and applies p to each of the members of ls. Like our definition of countdown, p takes one argument and yields a list, each of which is appended together. Do not use Racket’s built-in append-map in your definition.

    > (append-map countdown (countdown 5))
    (5 4 3 2 1 0 4 3 2 1 0 3 2 1 0 2 1 0 1 0 0)
  16. Define a procedure remove-last that takes a non-empty list and returns it with the last element removed. Empty lists can be considered bad data.

    > (remove-last '(2 6 3 8))
    (2 6 3)
Brainteasers
  1. In mathematics, the power set of any set S, denoted P(S), is the set of all subsets of S, including the empty set and S itself.

    S = \{x, y, z\}\\ \mathcal{P}(S) = \{\{\}, \{x\}, \{y\}, \{z\}, \{x,y\}, \{x,z\}, \{y,z\}, \{x,y,z\}\}.

    The procedure powerset takes a list and returns the power set of the members in the list. The exact order of your lists may differ, which for this problem is acceptable.

    > (powerset '(3 2 1))
    ((3 2 1) (3 2) (3 1) (3) (2 1) (2) (1) ())
    > (powerset '())
    (())
  2. The cartesian-product is defined over a list of sets (again simply lists that by our agreed upon convention don’t have duplicates). The result is a list of tuples (i.e. lists). Each tuple has in the first position a member of the first set, in the second position a member of the second set, etc. The output list should contain all such combinations. The exact order of your tuples may differ, which for this problem is acceptable.

    > (cartesian-product '((5 4) (3 2 1)))
    ((5 3) (5 2) (5 1) (4 3) (4 2) (4 1))
Just Dessert
  1. A quine is a program whose output is the listings (i.e. source code) of the original program. In Racket, 5 and #t are both quines.

    > 5
    5
    > #t
    #t

    We will call a quine in Racket that is neither a number nor a boolean an interesting Racket quine. Below is an interesting Racket quine.

    > ((lambda (x) (list x (list 'quote x)))
       '(lambda (x) (list x (list 'quote x))))
    ((lambda (x) (list x (list 'quote x)))
     '(lambda (x) (list x (list 'quote x))))

    Racket’s standard printing convention will prepend a quote to a list. You can hide the first quote by putting the following two lines of code before your quine:

    (print-reader-abbreviations #f)
    (print-as-expression #f)

    Define your own interesting Racket quine. The following should then be true.

    > (equal? quine (eval quine))
    #t
    > (equal? quine (eval (eval quine)))
    #t

    Not every Racket list is a quine. Make sure to use the above tests.