Lisp can be both fascinating and frustrating. Fascinating, because you can write compact code to solve really complex problems. Frustrating, because you can easily get lost in its maze of parentheses. I, for one, have been truly smitten by Lisp. My initial encounter with Lisp did not yield much success as I tried to come to terms with its strange syntax. The books I read on the Lisp language typically gloss over the exotic features of Lisp like writing Lisp code to solve the Towers of Hanoi or the Eight Queens problem. They talk about functions returning functions, back quotes and macros that can make your head spin.
I found this approach extremely difficult to digest the language. So I decided to view Lisp through the eyes of any other regular programming language like C, C++,, Java, Perl, Python or Ruby. I was keen on being able to do regular things with Lisp before I try out its unique features. So I decided to investigate Lisp from this view point and learn how to make Lisp do mundane things like an assignment, conditional, loop, array, input and output etc.
This post is centered on this fact.
Assignment statement
The most fundamental requirement for any language is to perform an assignment. For e.g. these are assignment statements in Lisp and its equivalent in C for e.g.
$ (setf x 5) - - - > ; $ x = 5
$ (setf x (+ (* y 2) (* z 8)) - - - -> $x = 2y + 8z
Conditional statement
There are a couple of forms of conditional statement in Lisp. The most basic is the ‘if’ statement which is special case. You can do if-then-else without the possibility of if-then-else if-else if - else
if (condition) statement else-statement
In Lisp this is written as
$(setf x 5)
$ (if (= x 5)
(setf x (+ x 5))
(setf (- x 6)))
10
In C this equivalent to
$ x = 5
$ if (x == 5)
x = x + 5;
else
x = x -6;
However Lisp allows the if-then-else if – else if –else through the use of the COND statement
So we could write
$ (setf x 10)
$ (cond ((< x 5) (setf x (+ x 8)) (setf y (* 2 y)))
((= x 10) (setf x (* x 2)))
(t (setf x 8)))
20
The above statement in C would be
$ x = 2
$ y = 10
$ if (x < 5)
{
x = x + 8;
y = 2 * y;
}
else if (x == 10)
{
x = x * 2;
}
else
x = 8;
Loops
Lisp has many forms of loops dotimes, dolist, do , loop for etc. I found the following most intuitive and best to get started with
$ (setf x 5)
$ (let ((i 0))
(loop
(setf y (* x i))
(when (> i 10) (return))
(print i) (prin1 y)
(incf i
)))
In C this could be written as
$ x = 5
(for i = 0; i < 10; i++)
{
y = x * i
printf(“%d %d\n”,i,y);
}
Another easy looping construct in C is
(loop for x from 2 to 10 by 3
do (print x))
In C this would be
(for x=2; x < 10; x = x+3)
print x;
Arrays
To create an array of 10 elements with initial value of 20
(setf numarray (make-array 10 :initial-element 20))
#(20 20 20 20 20 20 20 20 20 20)
To read an array element it is
$ (aref numarray 3) - - - numarray[3]
For e.g.
(setf x (* 2 (aref numarray 4))) - - - - x = numarray[4] * 2
Functions
(defun square (x)
(* x x))
This is the same as
int square (x)
{
return (x * x)
}
While in C you would invoke the function as
y = square (8)
In Lisp you would write as
(setf y (square 8))
Note: In Lisp the function is invoked as (function arg1 arg2… argn) instead of (function (arg1 arg2 … argn))
Structures
a) Create a global variable *db*
(defvar *db* nil)
b) Make a function to add an employee
$(defun make-emp (name age title)
(list :name name :age age :title title))
$(add-emp (make-emp "ganesh" 49 "manager"))
$(add-emp (make-emp "manish" 50 "gm"))
$(add-emp (make-emp "ram" 46 "vp"))
$ (dump-db)
For a more complete and excellent post on managing a simple DB looks at Practical Common Lisp by Peter Siebel
Reading and writing to standard output
To write to standard output you can use
(print “This is a test”) or
(print ‘(This is a test))
To read from standard input use
(let ((temp 0))
(print '(Enter temp))
(setf temp (read))
(print (append '(the temp is) (list temp))))
Reading and writing to a file
The typical way to do this is to use
a) Read
(with-open-file (stream "C:\\acl82express\\lisp\\count.cl")
(do ((line (read-line stream nil)
(read-line stream nil)))
((null line))
(print line)))
b) Write
(with-open-file (stream "C:\\acl82express\\lisp\\test.txt"
:direction :output
:if-exists :supersede)
(write-line "test" stream)
nil)
I found the following construct a lot easier
(let ((in (open "C:\\acl82express\\lisp\\count.cl" :if-does-not-exist nil)))
(when in
(loop for line = (read-line in nil)
while line do (format t "~a~%" line))
(close in)))
With the above you can get started on Lisp. However with just the above constructs the code one writes will be very “non-Lispy”. Anyway this is definitely a start.
Youre so cool! I dont think Ive read anything like this before. So good to find somebody with some original thoughts on this subject. Thanks for starting this up. This blog is something that is needed on the web someone with a little originality. Good job for bringing something new to the internet! http://scarprin.com/
ReplyDeleteJacob, thanks!
DeleteI really appreciate the kind of topics you post here. Thanks for sharing information that is actually helpful. Good day! http://celabright.com/
ReplyDeletegreat site. Keep doing.
ReplyDeleteClick Here http://www.scarprin.com/
I concur with your conclusions and will eagerly look forward to your future updates. The usefulness and significance is overwhelming and has been invaluable to me!
ReplyDeleteround dining tables for 4
It is very rare these days to find blogs that provide information someone is looking for. I am glad to see that your blog share valued information that can help to many readers. Thanks and keep writing!
ReplyDeletewww.fadeplex.com
hey that's really a great post and i like this and thanks for sharing it with us!I have read a few of the articles on your website now because I was looking for information aboutgames App. and I really like your style of blogging.
ReplyDeleteI agree with most of the points you make within this content.
ReplyDeleteEditing and Proofreading Services
Thesis Writing Services
Thesis Writing
I agree with most of the points you make within this content.
ReplyDeleteIT Consultants
IT Support Melbourne
IT Services Melbourne
Business IT Services
Hi,
ReplyDeleteI am very thankful too you for this sharing..It is very useful for my writing project I get enough information for my writing..!!!
SEO Expert
SEO Consultant
SEO Expert Pakistan
Cheap SEO Services
Thanks for sharing this informative blog with us..
ReplyDeleteMulti Skills Training
Forklift Training Melbourne
Civil Construction Courses
EWP Training Melbourne
Youre so cool! I dont think Ive read anything like this before. So good to find somebody with some original thoughts on this subject. Thanks for starting this up. This blog is something that is needed on the web someone with a little originality. Good job for bringing something new to the internet!
ReplyDeletewe buy ugly houses
My pleasure to being here on your blog that enhance my knowledge and good example for my writing projects.RH Stamps
ReplyDeleteTo be honest with you guys, the post was able to express what it wants to convey to the readers. Also, it has been a very effective approach which resulted to a profitable output for all who have been fortunate enough to come across it!
ReplyDeleteFreelance, Freelance job training in Bangladesh, Freelance job training in Dhaka, Freelance Training
Wow... another fantastic blog of tech. In this blog it tells us about Lisp. In this blog, we can very well understand HTML coding, C+ Programming from the very beginning. This post is supportive to many people who want to learn programming from the fresh...
ReplyDeleteWebsite Design