Principles of Programming Languages — Homework 5

Due by class time Tuesday, September 28

Reading

Exercises

  1. Write the function (times-ten* nums), which takes an arbitrarily-nested list of numbers and returns a new list with the same structure, but with each number multiplied by 10. Examples:

      (times-ten* '(1 2 3))  →  (10 20 30)
      (times-ten* '(1 (2 3) 4 (5)))  →  (10 (20 30) 40 (50))
      (times-ten* '((7 7 7) (1) ((2)) (3)))  →  ((70 70 70) (10) ((20)) (30))
      (times-ten* '(((9 (8 (7 6 5) 4) 3) 2) 1))  →  (((90 (80 (70 60 50) 40) 30) 20) 10)
      (times-ten* '(((25))))  →  (((250)))
    
  2. Write the function (addup-nums* ls), which takes an arbitrarily-nested list containing numbers and symbols, and adds up all of the numbers in the list, ignoring the symbols. Examples:

      (addup-nums* '())  →  0
      (addup-nums* '(1 (a 2) b (3 (c (4))) (d 5) e))  →  15
      (addup-nums* '(((apple 40) 30) (20 10))) → 100
      (addup-nums* '(w (x (y (z))))) → 0
      (addup-nums* '((((2 (4) (()) (6 8)))))) → 20
    
  3. Write the function (double* sym ls), which takes a symbol sym and an arbitrarily-nested list ls and "doubles" all of the sym's in ls. Examples:

    (double* 'hot '())  →  ()
    (double* 'hot '(texas (hot chili)))  →  (texas (hot hot chili))
    (double* 'fish '(one (fish (two (fish)))))  →  (one (fish fish (two (fish fish))))
    (double* 'double '(((double)) (toil) and (((trouble)))))  →  (((double double)) (toil) and (((trouble))))
    (double* 'a '(a 1 (and a 2) ((and ((a 3)))) and (a) 4))  →  (a a 1 (and a a 2) ((and ((a a 3)))) and (a a) 4)
    
  4. Write the function (swap* sym1 sym2 ls), which swaps all occurrences of the symbols sym1 and sym2 within an arbitrarily-nested list of symbols. Examples:

    (swap* 'red 'blue '((red fish) (blue) fish))  →  ((blue fish) (red) fish)
    (swap* 'orange 'blue '((red (fish (blue (fish))))))  →  ((red (fish (orange (fish)))))
    (swap* 'blue 'orange '(red (((fish blue))) fish))  →  (red (((fish orange))) fish)
    (swap* 'spam 'ham '((((green) eggs) and) ham))  →  ((((green) eggs) and) spam)
    (swap* 'eggs 'ham '((green) (eggs) and (ham) (and (eggs))))  →  ((green) (ham) and (eggs) (and (ham)))
    
  5. Write the function (x-ray ls), which takes an arbitrarily-nested list and returns a new version of it with the same internal structure, but with all elements replaced by x's. Examples:

      (x-ray '())  → ()
      (x-ray '(1 (a 2) b (3 (c (4))) (d 5) e))  → (x (x x) x (x (x (x))) (x x) x)
      (x-ray '(((apple 40) 30) (20 10))) → (((x x) x) (x x))
      (x-ray '(w (x (y (z))))) → (x (x (x (x))))
      (x-ray '((((2 (4) (()) (6 8)))))) → ((((x (x) (()) (x x)))))
    
  6. Write the function (ns-ray ls), which takes an arbitrarily-nested list and returns a new version of it with the same internal structure, but with all symbols replaced by s's and all numbers replaced by n's. Examples:

      (ns-ray '())  → ()
      (ns-ray '(1 (a 2) b (3 (c (4))) (d 5) e))  → (n (s n) s (n (s (n))) (s n) s)
      (ns-ray '(((apple 40) 30) (20 10)))  → (((s n) n) (n n))
      (ns-ray '(w (x (y (z)))))  → (s (s (s (s))))
      (ns-ray '((((2 (4) (()) (6 8))))))  → ((((n (n) (()) (n n)))))
    
  7. Write the function (flatten ls), which takes an arbitrarily-nested list, and returns a "flat" version of the list, with all of the elements in their original order, but without any inner parentheses. Examples:

      (flatten '())  →  ()
      (flatten '(1 (a 2) b (3 (c (4))) (d 5) e))  → (1 a 2 b 3 c 4 d 5 e)
      (flatten '(((apple 40) 30) (20 10))) → (apple 40 30 20 10)
      (flatten '(w (x (y (z))))) → (w x y z)
      (flatten '((((2 (4) (()) (6 8)))))) → (2 4 6 8)
    


Turning in Your Homework