Principles of Programming Languages — Homework 2

Due by class time Tuesday, September 14

Reading

Exercises

    Do these problems in the order shown.

  1. Write the function (any-numbers? ls), which takes a list ls and returns #t if the list contains at least one number, or #f otherwise. You can use the built-in Scheme function number? to test if something is a number. Examples:

      (any-numbers? '(c 3 p o))  →  #t
      (any-numbers? '(x y z))  →  #f
      (any-numbers? '(a b c d 7 8))  →  #t
      (any-numbers? '(1 2 3 4 5))  →  #t
      (any-numbers? '(2 b or not 2 b))  →  #t
      (any-numbers? '(apple))  →  #f
      (any-numbers? '(9))  →  #t
      (any-numbers? '())  →  #f
    
  2. Write the function (all-numbers? ls), which takes a list ls and returns #t if all elements in the list are numbers, or #f otherwise. If the list is empty, the function should return #f, not #t. Hint: include a separate line in your cond for one-element lists, using an and expression. Examples:

      (all-numbers? '(c 3 p o))  →  #f
      (all-numbers? '(x y z))  →  #f
      (all-numbers? '(a b c d 7 8))  →  #f
      (all-numbers? '(1 2 3 4 5))  →  #t
      (all-numbers? '(2 b or not 2 b))  →  #f
      (all-numbers? '(apple))  →  #f
      (all-numbers? '(9))  →  #t
      (all-numbers? '()) → #f
    
  3. Write the function (rac ls), which takes a non-empty list ls and returns the last element in the list (rac is the "mirror image" of car). Hint: if the list has only one element, then just return that element. Examples:

      (rac '(a b c d e))  →  e
      (rac '(apple))  →  apple
      (rac '(banana mango shake yum))  →  yum
      (rac '(x y))  →  y
    
  4. Write the function (shorten n ls), which takes a number n and a list ls containing at least n elements, and returns a new shorter version of the list with the first n elements removed from the front. You may assume that n will never be greater than the length of ls. Hint: you can check if n equals 0 by writing either (= n 0) or (zero? n). Examples:

      (shorten 1 '(c 3 p o))  →  (3 p o)
      (shorten 0 '(x y z))  →  (x y z)
      (shorten 3 '(a b c d 7 8))  →  (d 7 8)
      (shorten 5 '(1 2 3 4 5))  →  ()
      (shorten 2 '(2 b or not 2 b))  →  (or not 2 b)
    
  5. Write the function (nth n ls), which takes a number n and a non-empty list ls and retrieves the nth element of the list, counting from 0. You may assume that n will always be less than the length of ls. Examples:

      (nth 0 '(a b c d e))  →  a
      (nth 3 '(a b c d e))  →  d
      (nth 2 '(b c d e))  →  d
      (nth 1 '(x y))  →  y
      (nth 6 '(one fish two fish red fish blue fish))  →  blue
    
  6. Write the function (remove-first-num ls), which takes a list and returns a new list with the first number removed. Hint: use the function rember from The Little Schemer as a model. Examples:

      (remove-first-num '(c 3 p o))  →  (c p o)
      (remove-first-num '(x y z))  →  (x y z)
      (remove-first-num '(a b c d 7 8))  →  (a b c d 8)
      (remove-first-num '(1 2 3 4 5))  →  (2 3 4 5)
      (remove-first-num '(2 b or not 2 b))  →  (b or not 2 b)
      (remove-first-num '(9))  →  ()
      (remove-first-num '(z))  →  (z)
    
  7. Write the function (remove-all-nums ls), which takes a list and returns a new list with all numbers removed. Hint: use the function multirember from The Little Schemer as a model. Examples:

      (remove-all-nums '(c 3 p o))  →  (c p o)
      (remove-all-nums '(x y z))  →  (x y z)
      (remove-all-nums '(a b c d 7 8))  →  (a b c d)
      (remove-all-nums '(1 2 3 4 5))  →  ()
      (remove-all-nums '(2 b or not 2 b))  →  (b or not b)
      (remove-all-nums '(9))  →  ()
      (remove-all-nums '(z))  →  (z)
    
  8. Write the function (x-nums ls), which takes a list and returns a new list with all of the numbers replaced by the literal symbol xxx. Hint: use the function multisubst from The Little Schemer as a model. Examples:

      (x-nums '(c 3 p o))  →  (c xxx p o)
      (x-nums '(x y z))  →  (x y z)
      (x-nums '(a b c d 7 8))  →  (a b c d xxx xxx)
      (x-nums '(1 2 3 4 5))  →  (xxx xxx xxx xxx xxx)
      (x-nums '(2 b or not 2 b))  →  (xxx b or not xxx b)
      (x-nums '(9))  →  (xxx)
      (x-nums '(z))  →  (z)
    
  9. Write the function (rdc ls), which takes a non-empty list ls and returns a new list containing all of the elements of ls except the last one (rdc is the "mirror image" of cdr). Examples:

      (rdc '(a b c d e))  →  (a b c d)
      (rdc '(apple))  →  ()
      (rdc '(banana mango shake yum))  →  (banana mango shake)
      (rdc '(x y))  →  (x)
    
  10. Write the function (snoc x ls), which takes an element x and a list ls, and returns a new list with x added to the end of ls (snoc is the "mirror image" of cons). Hint: if the list is empty, return a new list containing x, as in the fourth example below. Examples:

      (snoc 'another '(a b c d e))  →  (a b c d e another)
      (snoc 'sauce '(pepperoni pizza))  →  (pepperoni pizza sauce)
      (snoc 'yum! '(banana mango shake yum))  →  (banana mango shake yum yum!)
      (snoc 'whatever '())  →  (whatever)
    


Turning in Your Homework