Programming Languages -- Spring 1996

Homework One: Basic Scheme

Due Monday, January 15, at 9:00am

  1. Write the function member-twice? that returns true if a particular atom appears at least twice in a list of atoms:
    > (member-twice? (quote c) (quote (a b c d d b a)))
    #f
    > (member-twice? (quote b) (quote (a b c d d b a)))
    #t
    
    
  2. Write the function rember2 that takes an atom a and a list of atoms ls, and removes the second occurrence of a in ls.
    > (rember2 (quote a) (quote (a b d a c)))
    (a b d c)
    > (rember2 (quote a) (quote (a b d c)))
    (a b d c)
    
    
  3. Write the function multtup that takes a list of numbers (a tuple) and multiplies all the numbers in the list together.
    > (multtup (quote (3 2 4)))
    24
    > (multtup (quote (6 2 1)))
    12
    > (multtup (quote ()))
    1
    
    
  4. Write the function list-index that takes an atom a and a list ls, and returns the zero-based index of a in ls. If the atom a does not occur in ls, it should return -1. This is a difficult exercise.
    > (list-index (quote a) (quote (b a c)))
    1
    > (list-index (quote d) (quote (b a c)))
    -1
    
    
  5. Write the function tree-mult that takes a possibly deep list of numbers and multiplies all the numbers together.
    > (tree-mult (quote ((1 2) 3 (((4))))))
    24
    > (tree-mult (quote ((1) (2 3 4) 5 6)))
    720
    
    
  6. Write the function duplicate that takes a number n and an atom a and returns a list containing only n occurrences of a.
    > (duplicate 3 'foo)
    (foo foo foo)
    > (duplicate 0 'foo)
    ()
    
    

  7. Write the function compose2 that takes two unary functions f and g, and returns a unary function that is the composition of f and g.
    > ((compose2 zero? sub1) 2)
    #f
    > ((compose2 zero? sub1) 1)
    #t
    
    

  8. Write the function assq that takes an atom a and an association list (a list of non-empty lists) als, and returns the first list in als whose car is a. If there are no lists in als whose car is a, assq returns #f.
    > (assq 'g '((b c) (d f g) (g c 3) (f (g h))))
    (g c 3)
    > (assq 'g '((b c) (d f g) (f (g h))))
    #f
    
    

  9. Write the function list-ref that takes a number n and a list ls of length at least n+1, and returns the (zero-based) nth element in ls.
    > (list-ref 3 '(a b c d e f))
    d
    > (list-ref 0 '(a b c d))
    a
    
    

  10. Write the function snoc that takes a list ls and a datum i, and returns a new list where i has been added to the end of ls.
    > (snoc '(a b c) '(x y))
    (a b c (x y))
    > (snoc '() 'a)
    (a)
    
    

  11. Write the function intersection that takes two sets (lists of symbols without duplicates) s0 and s1, and returns a new set containing all elements that are in both s0 and s1.
    > (intersection '(a b c) '(b c d))
    (b c) or (c b)
    > (intersection '(a b c) '(d e f))
    ()
    
    

  12. Write the function count-parens that takes a possibly deep list ls and returns the number of parentheses in the printed representation of the list.
    > (count-parens '((a b) (()) c))
    8
    > (count-parens '())
    2
    
    

  13. Write the function sum-of-squares that takes a tuple (list of numbers) tup and returns the sum of the squares of the numbers.
    > (sum-of-squares '())
    0
    > (sum-of-squares '(3))
    9
    > (sum-of-squares '(3 4))
    25
    
    

Submission

Write your answers to the exercises in a file (with comments, following the proper indentation rules), and send that file to

c311@lakshmi.cs.indiana.edu
with the subject line
01
Assuming you've saved your homework in the file ``one.ss'' in the current directory, one way to submit is with the command:
Mail -s "01" c311@lakshmi.cs.indiana.edu < one.ss

Back to the c311 page

ehilsdal@cs.indiana.edu
Sun Jan 7 23:32:49 EST 1996