Recursive Tree Rabbit Holes

  1. WHY? When using my own home grown object system, I found out that it could not handle keyword parameters.
  2. Problem. Take a parameter list and remove keyword parameters.
  3. Solution. Parse the argument list to the functions, remove and if needed replace keyword.
  4. Example. Need to remove &key and replace with keyword and variable.
    • (a b) => (a b)
    • (a (b)) => (a (b))
    • (&key a) => (:a a)
    • (&key a b) => (:a a :b b)
    • (a &key b) => (a :b b)
    • (a (&key b)) => (a (:b b))

Scope

The number one lesson for this type of development is to follow an iterative approach with simple use cases. As the use cases pass the unit tests, implement new use cases.

Step 1

(defun replace-key-args* (args)
  (labels ((rka-aux (args)
             (if (atom args)
                 args
                 (cons (rka-aux (first args))
                       (if (rest args)
                           (rka-aux (rest args)))))))
    (rka-aux args)))

(test-fixture
 replace-key-args*
 (:tests
   (should-replace-key-args
    (assert-equal '(a) (replace-key-args* '(a)))
    (assert-equal '(a b) (replace-key-args* '(a b)))
    (assert-equal '(a b (c d) e f) (replace-key-args* '(a b (c d) e f)))))

Step 2

  • Add new use case, (&key a).
  • Add a test for the &key keyword. Replace it with the keyword symbol generated from the symbol following &key symbol.
  • Making a keyword is a matter of simply interning a string into the “KEYWORD” package.
(defun make-keyword (name)
  (values (intern (string-upcase name) "KEYWORD")))

(defun replace-key-args** (args)
  (labels ((rka-aux (args)
             (if (atom args)
                 args
                 (progn
                   (if (equal '&key (first args))
                       (setf (first args) (make-keyword (second args))))
                   (cons (rka-aux (first args))
                         (if (rest args)
                             (rka-aux (rest args))))))))
    (rka-aux args)))

(test-fixture
 replace-key-args**
 (:tests
   (should-replace-key-args
    (assert-equal '(a) (replace-key-args** '(a)))
    (assert-equal '(a b) (replace-key-args** '(a b)))
    (assert-equal '(a b (c d) e f) (replace-key-args** '(a b (c d) e f)))
    (assert-equal '(:a a) (replace-key-args** '(&key a))))))

Step 3

  • Another Use Case (&key a b).
  • Need to add a parameter to the recursive algorithm. When we replace the &key keyword and then pass the modified ‘args variable, to the ‘replace-key-args**’, it no longer knows that a &key keyword was in the ‘args list.
(defun replace-key-args*** (args)
  (labels ((rka-aux (args is-key-arg?)
             (if (atom args)
                 args
                 (progn
                   (if (equal '&key (first args))
                       (progn
                         (setf is-key-arg? t)
                         (setf args (rest args))))
                   (cond
                     (is-key-arg?
                      (cons (rka-aux (make-keyword (first args)) nil)
                            (cons (rka-aux (first args) nil)
                                  (if (rest args)
                                      (rka-aux (rest args) t)))))
                     (t
                      (cons (rka-aux (first args) nil)
                            (if (rest args)
                                (rka-aux (rest args) nil)))))))))
    (rka-aux args nil)))

(test-fixture
 replace-key-args***
 (:tests
   (should-replace-key-args
    (assert-equal '(a) (replace-key-args*** '(a)))
    (assert-equal '(a b) (replace-key-args*** '(a b)))
    (assert-equal '(a b (c d) e f) (replace-key-args*** '(a b (c d) e f)))
    (assert-equal '(:a a) (replace-key-args*** '(&key a)))
    (assert-equal '(:a a :b b) (replace-key-args*** '(&key a b))))))

Step 4

  • Add new Use Case (a &key b).
  • No modification of code required.

Step 5

  • Add All Use Cases.
  • There is more use cases than at the head.
  • This algorithm is ripe for some refactoring, but it works.
(defun replace-key-args**** (args)
  (labels ((rka-aux (args is-key-arg?)
             (if (atom args)
                 args
                 (progn
                   (if (equal '&key (first args))
                       (progn
                         (setf is-key-arg? t)
                         (setf args (rest args))))
                   (cond
                     ((consp (first args))
                      (cons (rka-aux (first args) nil)
                            (if (rest args)
                                (rka-aux (rest args) nil))))
                     (is-key-arg?
                      (cons (rka-aux (make-keyword (first args)) nil)
                            (cons (rka-aux (first args) nil)
                                  (if (rest args)
                                      (rka-aux (rest args) t)))))
                     (t
                      (cons (rka-aux (first args) nil)
                            (if (rest args)
                                (rka-aux (rest args) nil)))))))))
    (rka-aux args nil)))

(test-fixture
 replace-key-args****
 (:tests
   (should-replace-key-args
    (assert-equal '(a) (replace-key-args**** '(a)))
    (assert-equal '(a b) (replace-key-args**** '(a b)))
    (assert-equal '(a b (c w) r s t) (replace-key-args**** '(a b (c w) r s t)))
    (assert-equal '(:a a) (replace-key-args**** '(&key a)))
    (assert-equal '(:a a :b b) (replace-key-args**** '(&key a b)))
    (assert-equal '(a :b b) (replace-key-args**** '(a &key b)))
    (assert-equal '(:a a :b b (c d) e f) (replace-key-args**** '(&key a b (c d) e f)))
    (assert-equal '(a b (:c c :d d) e f) (replace-key-args**** '(a b (&key c d) e f)))
    (assert-equal '(a b (c :d d) e f) (replace-key-args**** '(a b (c &key d) e f)))
    (assert-equal '(a b (c d) :e e :f f) (replace-key-args**** '(a b (c d) &key e f)))
    (assert-equal '(:a a :b b (c d) :e e :f f) (replace-key-args**** '(&key a b (c d) &key e f)))
    (assert-equal '(:a a :b b (:c c :d d) :e e :f f) (replace-key-args**** '(&key a b (&key c d) &key e f)))
    (assert-equal '(a :b b (c :d d) e :f f) (replace-key-args**** '(a &key b (c &key d) e &key f)))))

Happy Monday!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: