CS 112: Lisp Exercises for August 30 (TTh class) and 31 (MWF class)

  1. Write a firstatom function that returns the first atom in a list, regardless of whether the given list is nested or not. For example:
    ( firstatom '(a b c) ) => a
    ( firstatom '((x y z) (a (b)) c) ) => x
    ( firstatom '((((1 a) b) (2 c) d) (3 4 e f) ) => 1
    
  2. Write an allfirsts function takes a single list argument and returns a list containing all leading atoms of the elements in its argument (use firstatom in item 1). For example:
    ( allfirsts '((1 2 3) (a b c) (x y z)) ) => (1 a x)
    ( allfirsts '(a b c) ) => (a b c)
    ( allfirsts '((1 2 3) ((x y) z) a ((4 5)(6) 7 (8 9))) ) => (1 x a 4)
    
  3. Selection sort of a list can be recursively defined as cons-ing the minimum value in the list with (the rest of the list, without the minimum, sorted). Complete the following selection sort implementation in Lisp by implementing a getmin function and a removeelement function. Try selsort on the some examples such as:
    ( selsort '(3 1 4 5 2 6) ) => (1 2 3 4 5 6)
    ( selsort '(100 200 50 32 65 12 84) ) => (12 32 50 65 84 100 200)
    ( selsort '(3 1 4 1 5 6 5 5 1 2 6) ) => (1 1 1 2 3 4 5 5 5 6 6)