December 22, 2008
Recursive Condition
This little trick hit me when I was perusing PCL. Specifically, the when selector.
(defun rec-cond (key)
(cond
((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 »
4 Comments |
Domain Specific Language, Lisp Programming | Tagged: dsl, Lisp Programming, Syntactic Sugar |
Permalink
Posted by gutzofter
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)
;>>(NIL 3 NIL NIL 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 »
Leave a Comment » |
Lisp Programming, OOP | Tagged: Lisp, Lisp Programming, OOP |
Permalink
Posted by gutzofter
December 11, 2008
This is a must have. Creating a list of the links to review later instead of opening tabs.
Read It Later
Leave a Comment » |
Uncategorized | Tagged: Promotional |
Permalink
Posted by gutzofter
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:
(should-maintain-scope-of-inner-property
(with-property (value 24)
(value :is (incf (value))))
(assert-equal 25 (value)))
‘incf’ is a destructive operation.
Read the rest of this entry »
Leave a Comment » |
Lisp Programming | Tagged: Abstraction, Liskov Substitution Principle, Lisp Programming, OOP, Syntatic Sugar |
Permalink
Posted by gutzofter
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 »
1 Comment |
EDA, Lisp Programming, Uncategorized | Tagged: Abstraction, Encapsulation, Lisp Programming, Macro |
Permalink
Posted by gutzofter
December 7, 2008
Getter/Setters
How many times have you created two function for setting a specific value and then getting the value?
(let ((value ""))
(defun property (&key is)
(cond
(is (setf value is))
(t value)))
;;; here is getter and setter functions
(defun get-property ()
value)
(defun set-property (data)
(setf value data)))
(define-test get_set-property
(assert-equal "" (get-property))
(assert-equal "property" (set-property "property"))
(assert-equal "property" (get-property))
(assert-equal "" (set-property "")))
(define-test property
(assert-equal "" (property))
(assert-equal "property" (property :is "property"))
(assert-equal "property" (property))
(assert-equal "" (property :is ""))
Using a &key keyword parameter adds syntatic sugar to setting properties. It also means less typing.
It is done with a conditional. Another benefit is that behavior for the property is located in only one function instead of two functions.
New Use Case
(Zach Beane’s Request)
Need to be able to set the property to nil. Added the test to my unit tests. It failed. Modified the code and now it passes.
You just need to add a supplied-p parameter.
(let ((value ""))
(defun property (&key (is nil is-p))
(cond
(is-p (setf value is))
(t value))))
(define-test property
(assert-equal "" (property))
(assert-equal "property" (property :is "property"))
(assert-equal "property" (property))
(assert-equal "" (property :is ""))
(assert-equal nil (property :is nil)))
Happy Lisp Cargo-Culting!
7 Comments |
Lisp Programming | Tagged: Lisp, Lisp Programming, Syntactic Sugar |
Permalink
Posted by gutzofter
December 5, 2008
I ran across this post, Prototyping Genetic Algorithms in Lisp.
Check it out. It looks to be more along the lines of Genetic Programming than Genetic Algorithms.
His code looks really good. Very readable! The methods are small, a sign of well factored code.
2 Comments |
Lisp Programming | Tagged: Genetic Algorithm, Genetic Programming, Lisp Programming |
Permalink
Posted by gutzofter
December 4, 2008
References for our discussion:
Requirements for our EDA:
- Storage of events to be executed when events triggered.
- Events.
- Push/Pull Model.
- Broadcast of events. One trigger can execute more than one handler.
- Ex. When User Clicks Save Button, data is saved and the event is logged as executed.
Read the rest of this entry »
1 Comment |
EDA, Lisp Programming | Tagged: EDA, Event Driven Architecture, Event Driven Programming, Events, Lisp Programming |
Permalink
Posted by gutzofter