I’m a newbie to Lisp programing language. Lisp is one of the most oldest programming language that is still being used today. People have a lot of defences on programming languages, because there are too many choices to us today. Every language has their strength and weakness. It is not easy to make the choice.
In my opinion, the best way to decide what languages I must learn is to see which kinds of people are loving it. For example, Ruby community does great supports for business plans; Python is the best open sourced language for academia uses (scientific libraries, machine learning, computer vision, statistics, visualizations…etc.) Many old school hackers (including me) still love to use Perl; Java is widely used in big companies; VBA is friendly for dealing excel data…etc. And Lisp, are highly recommended by great hackers.
If you are also interested in Lisp, I recommend Practical Common Lisp written by Peter Seibel, and On Lisp written by Paul Graham. Both of them are free, downloadable PDF files, and there are also online version and epub format.
Basic thoughts
Let’s begin with basic structures. At first, I design the structure similar to what you will write in C code.
1 2 3 4 5 |
|
Here we can see the power of Lisp. It does not only create the structure, but
also create constructor and accessors for the structure. Now you can create the
rb
and access it like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This seems to be nice. But we can rewrite it in more lispy style:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The s-expression with a preceding `
means that it is a expression that
lisp don’t evaluate, except expression inside it with a preceding ,
. It is
convenient for us to generate new list object.
To access the elements in a list:
car
the firstcadr
the secondcaddr
the third
And now we can wirte a persistant basic binary insert function!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Let’s exam the function:
1 2 3 4 5 6 7 8 9 10 11 |
|
Wait! Hold on!
The code is god damn ugly! We can fix it with some utilities:
1 2 3 4 5 6 7 8 9 |
|
The kid
takes a node and a series of T
and nil
; When it saw a T
, it
returns the right node, or it will return the left one. For example:
1 2 3 4 5 6 |
|
Using nil
and T
as left and right dirctions, it is easier for us to rewrite
binary-insert-r
because we can change left and right cases into variables.
1 2 3 4 5 6 7 8 9 |
|