Tuesday, 11 September 2012

Exercise 182: Eliminate quote from the following expressions


Exercise 182: Eliminate quote from the following expressions

Eliminate quote from the following expressions so that they use list instead:
  1. '(1 "a" 2 #f 3 "c")
  2. '()
  3. and this table-like shape:
    '(("alan" 1000)
      ("barb" 2000)
      ("carl" 1500)
      ("dawn" 2300))


    Now eliminate list in favor of cons where needed.

Eliminate in favour of list


This is very straight forward.

(list 1 "a" 2 false 3 "c")
(list)
(list (list "alan" 1000)
      (list "barb" 2000)
      (list "carl" 1500)
      (list "dawn" 3000))


Eliminate in favour of cons


This is not hard, but painful. Constructing lists like this is frustrating - which is the point of the exercise.

(cons 1 (cons "a" (cons 2 (cons false (cons 3 (cons "c" empty))))))

empty


(cons (cons "allan" (cons 1000 empty)) 
      (cons (cons "barb" (cons 2000 empty))  
            (cons (cons "carl" (cons 1500 empty)) 
                  (cons (cons "dawn" (cons 3000 empty)) 
                   empty  ))))



No comments:

Post a Comment