Lisp Programming Trick #2

December 22, 2008

Recursive Condition

This little trick hit me when I was perusing PCL. Specifically, the when selector.

(defun rec-cond (key)
    ((eq key :before) (format nil "executing before" ))
    ((eq key :during) (format nil "executing during" ))
    ((eq key :after) (format nil "executing after" ))

    ((eq key :set-up) (rec-cond :before))
    ((eq key :init) (rec-cond :before))

    ((eq key :teardown) (rec-cond :after))

    (t (error (format nil "unknown command: ~A" key))))

This overloads keyword symbols so that you more than one keyword can execute the same code. Read the rest of this entry »

Test Driven Development As an Introduction

December 19, 2008

I recommend Clarke Ching’s TDD Test Drive for an introduction for TDD.

It is pretty good.

Have Fun!

An Object in Lisp. Part 5

December 18, 2008

Destructuring Bind

… binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from the evaluation of expression; then destructuring-bind evaluates forms.

‘destructuring-bind‘ is a wonderful macro. The first thing you do is hand it a lambda list and a list of values. It acts like a pattern matcher. If the list of values does not match up to the lambda list it will throw an error.

Here is what I’m talking about:

(defun test-d-bind (&rest args)
  (destructuring-bind (a &optional (b 3) &rest x &key c (d a)) args
    (list a b c d x))
(test-d-bind nil)
(test-d-bind 1 6 :d 8 :c 9 :d 10)
;>>(1 6 9 8 (:D 8 :C 9 :D 10)

Read the rest of this entry »

Read it Later – Make your browsing a breeze

December 11, 2008

This is a must have. Creating a list of the links to review later instead of opening tabs.

Read It Later

And Behind Door Number 2 Is . . . A Leaky Abstraction

December 11, 2008

Liskov Substitution  Principle (LSP):

Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.

My Bogus was an attempt to abstract a method by which I could get access to a variable using a syntatic-sugar idiom. The abstraction leaks. Here is how it leaks:

       (with-property (value 24)
         (value :is (incf (value))))
       (assert-equal 25 (value)))

‘incf’ is a destructive operation.

Read the rest of this entry »

Abstraction of an encapsulted property in Lisp

December 9, 2008

Abstraction (computer science): is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time.

If you read Lisp Trick #1, hopefully, you saw a pretty good abstraction. What it was doing was abstracting out the setting and getting a value from a variable. As a bonus, because of the way &key parameter keyword can be used, we were able to differentiate whether you were getting the value or setting the variable. Another bonus is that the variable we are getting and setting is encapsulated.

Usually an abstraction is created with the use of macros. Now I’ve made an even larger abstraction of the setter/getter abstraction.

Read the rest of this entry »

Recursive Tree Rabbit Holes

December 8, 2008
  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))

Read the rest of this entry »