April 9, 2006
Nothing scares people away from learning a new programming language more than crazy syntax. It has been said that Perl looks like a cartoon character swearing. Python uses whitespace for code branching (the horror!). Ruby uses a mix of '{}' and 'begin' 'end' (ack!). Its a real shame programmers get so set in their ways and they cannot look past these simple things. I am certainly guilty of this. When I started learning python I really struggled with the whitespace issue. I stuck with it though and before I knew it I realized any good programmer indents their code to look pretty. Python just uses that convention to force good code organization and eliminate the need for {}. There is so much power in python that I simply put the whitespace issue in the back of my mind, way back, and started coding productively. I've learned that if you dismiss a language simply based on syntax weirdness you could be missing out on something big.
Over the last year I've enjoyed learning Common Lisp. Once again, the parenthesis were the first hurdle I had to get over. I would have nightmares about balancing issues and horrible hard to find bugs resulting from it. As it turns out, parens are really quite cool. In Lisp, code is data is code. This is a very powerful concept that doesn't exist in many other languages. You can manipulate code as if it were data. Add macros on top of that and you get an almost limitless way to extend Lisp into anything you want.
The paren balancing issue is solved with a good text editor like Emacs. When you cursor over a paren the opposite paren is highlighted (most editors do this). Emacs will also indent your code neatly according to the parentheses. If you see your indenting becomes wacky you know you have parens out of balance. In practice this just does not happen. Ever. Since your code is data, emacs can easily parse the code and give you nice hints about function call parameters. Code navigation is made easier as well. You can navigate quickly into, out of, and over the code structure (s-expressions) with simple keystrokes. If you like avoiding the mouse as much as I do, this is invaluable. So with those advantages, parentheses become your new best friend!
You can also think of parentheses as eliminating the need for other types of punctuation. Languages like C#, Java, C++, etc. use punctuation ;{}[]() to structure code. Lisp only uses parentheses, which makes it more concise (IMHO). For a comparison, here's the Fibonacci sequence function implemented in Common Lisp and Java.
;; Common Lisp
(defun fib (n)
(if (< n 2)
n
(+ (fib (- n 1))
(fib (- n 2)))))
//Java
public class Fibonacci
{
public static long fib(int n)
{
if (n < 2)
{
return n;
}
else
{
return fib(n - 1) + fib(n - 2);
}
}
}
1 Comment |
Lisp, Programming |
Permalink
Posted by anthonyf
April 7, 2006
I went to a free seminar yesterday with my good friend Keven called "Pattern Oriented Development", hosted by Net Objectives. Overall the seminar was good and was clearly a lure to get you into one of their paid seminars. One of the patterns used was the Strategy pattern, which I didn't know by name but once it was described to me I new exacly what it was. A good definition of the strategy pattern can be found on wikipedia. The implementation of this pattern can be a bit involved if you are using a main-stream language like C++, Java or C#. It involves the following:
- Create an abstract base class for the strategy
- Create each derived concrete strategy class
- Create a factory that knows how to create each concrete strategy class.
There was a great UML class diagram presented in the class describing the above items. You'll just have to take my word for it.
Common Lisp has a concept called generic methods, which essentially implements the Strategy pattern. The macro DEFMETHOD is used to define a generic method. It handles all of the nasty details of running the appropriate strategy or implmentation based on the type of arguments passed into it. Don't be fooled into thinking generic methods are overloaded functions. They are much more than that. With generic methods, creating a factory that knows how to create each concrete strategy class is unecessary.
The wikipedia link above shows an example of the strategy pattern written in C# which I have re-written in Common Lisp. It is worth noting that the base strategy class as well as the DEFGENERIC declaration are unecessary for the strategy pattern to work. I left them in to make the comparison with the C# code easier for people who dont understand Lisp. Here's my code:
(defclass strategy ()()
(:documentation "Base class strategy"))
(defclass concrete-strategy-a (strategy)()
(:documentation "Strategy A"))
(defclass concrete-strategy-b (strategy)()
(:documentation "Strategy B"))
(defclass concrete-strategy-c (strategy)()
(:documentation "Strategy C"))
(defgeneric execute (strategy)
(:documentation "A generic method defines the interface"))
(defmethod execute ((strategy concrete-strategy-a))
"Executes Strategy A"
(format t "Called concrete-strategy-a~%"))
(defmethod execute ((strategy concrete-strategy-b))
"Executes Strategy B"
(format t "Called concrete-strategy-b~%"))
(defmethod execute ((strategy concrete-strategy-c))
"Executes Strategy C"
(format t "Called concrete-strategy-c~%"))
(defun main ()
;create the strategies
(let ((strategies (list (make-instance 'concrete-strategy-a)
(make-instance 'concrete-strategy-b)
(make-instance 'concrete-strategy-c))))
;execute the strategies
(loop for strategy in strategies
do (execute strategy))))
;;;; OUTPUT
;;;
; CL-USER> (main)
;; Called concrete-strategy-a
;; Called concrete-strategy-b
;; Called concrete-strategy-c
;; NIL
3 Comments |
Lisp, Programming |
Permalink
Posted by anthonyf
April 7, 2006
Coming up with unique blog names is a real PITA! I spent several days googling name ideas only to discover someone had already thought of them. I was sure that “blogrammer” would be unique but google turned up 111 results. The title of this post is one of the worst titles I came up with. I finally settled on the slightly meaningful yet arguably lame title you see above. It is based on a book that I really enjoy reading called The Pragramatic Programmer by Dave Thomas and Andy Hunt. If you are a programmer and have not read it, go buy it. Right now. It’s chock-full of all kinds of programming wisdom.
So what is this weblog about? It is mainly a tool for me to collect ideas related to my career as a computer programmer. I hope post a bit of code and write about new technologies or techniques that I discover. I’ve kept a non-public pseudo journal of things that I’ve learned in the past so hopefully that stuff will somehow get merged into this blog. Another goal is to make an effort to share information with my programmer friends.
1 Comment |
Uncategorized |
Permalink
Posted by anthonyf