CSE 341 -- Assignment 1 -- Scheme Warmup

Due in lecture October 9, 1998

All of these problems should be done without using side effects (i.e. redefining variables or using set!).

  1. Write and test a Scheme function pythagoras that takes the lengths of the two legs of a right triangle and returns the length of the hypotenuse. For example:
    (pythagoras 3.0 4.0)  => 5.0
    
  2. Write and test a Scheme function no-duplicates that takes a list of numbers as an argument, and returns a new list which is like the argument but with duplicate elements dropped. The order of the elements in the returned list doesn't matter. For example:
    (no-duplicates '(1 3 3 8 5 3 5))  => (1 3 8 5)
    (no-duplicates '(1 3 5))  => (1 3 5)
    (no-duplicates ())  => ()
    

  3. Write and test Scheme functions contains, add, and delete to manipulate binary search trees. contains should take a tree and an element as arguments, and return true or false depending on whether the tree contains the element. add should take a tree and an element as arguments, and return a new tree with the element added. delete should take a tree and an element as arguments, and return a new tree with the element deleted. For both add and delete, the existing tree that is the argument should not be altered.

    If you try to add an element to a tree that already contains that element, just return the tree (don't make a new tree). If you try to delete an element from a tree that doesn't contain it, signal an error using

    (error "element not found")
    

    You should choose an appropriate representation for binary trees (hint: represent a node in the tree as a list with 3 elements).