Monthly Archives: March 2013

Stepping into Lisp programming

Actually I’m not going to provide a series of Lisp tutorials here. If you want to learn Lisp and it’s syntax I would like to recommend you to follow these guides Lisp tutorial, Common Lisp – Introduction, Tutorials and Resources. Go there and study it first.

If you still don’t know why Lisp is called an awesome language see the previous post.

The best way to learn programming is just doing it. Learn to do it by doing it! If you are an amateur to the Lisp programming (even though me too) you can try out the below working program yourself now.

Download GNU CLISP 2.49. Setup it, launch it. Startup screen is as below.

clisp-startup-screen

Lisp is works as an interpreter. You can type lisp commands on the console directly and run them. Either you can make an external Lisp file by using a simple text editor and run it on the Clisp console.

Create the following Lisp file in notepad. This is my first Lisp program created: A prime number finder, which I wrote as my very first Lisp program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(defparameter isprime 1)
 
(defun prime-numbers (from to)
(if (> from to) nil
(progn
(devider from from)
(if (= isprime 1) (print from))
(setf isprime 1)
(prime-numbers (+ from 1) to))))  
 
(defun devider(num val)
(if (< num 3) nil 
(progn
(if (= (mod val (- num 1)) 0) (setf isprime 0))
(devider (- num 1) val)))
)
(defparameter isprime 1)

(defun prime-numbers (from to)
(if (> from to) nil
(progn
(devider from from)
(if (= isprime 1) (print from))
(setf isprime 1)
(prime-numbers (+ from 1) to))))  

(defun devider(num val)
(if (< num 3) nil 
(progn
(if (= (mod val (- num 1)) 0) (setf isprime 0))
(devider (- num 1) val)))
)

The above lisp program consist of two functions (prime-numbers, devider) and one global variable (isprime). Actually the “devider” functions is called by the “prime-numbers” function internally. So the main function that we use to run the program is “prime-numbers”. It has two parameters called “from” and “to” which use to pass start-point and ending-point of the numbers range when we call it.

Copy the above code into an text editor and save the file as “prime-num-finder.lisp”. Remember to save it with the “.lisp” extension.

saving-lisp-file

Turn back to the Clisp console. Use the following command to load the lisp file you created. Remember to add an additional “\” for each back slash on the file path.

(load “C:\\prime-number-finder.lisp”)

loading-lisp-file

The “load” command exactly works as if you had typed in the code directly at the command line. After loading the file successfully, the “prime-number” function on the lisp file can be called as (prime-numbers [start point] [ending point]). Let’s try out to find the prime numbers between 1 and 10.

(prime-numbers 1 10)

calling-method

The numbers given to the function parameters are 1 and 1o. So the output prime numbers displaying as 1, 2, 3, 5, 7.

If God used a programming language, it would be Lisp!

john-mccarthy

Some of the quotes I’ve found on the internet which urged me to start learning Lisp!

“A language that doesn’t affect the way you think about programming, is not worth knowing.” – Alan Perlis

“Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.” – Eric Raymond, “How to Become a Hacker”

“Like DNA, such a language [Lisp] does not go out of style.” – Paul Graham, ANSI Common Lisp

“The problem with Lisp is that it makes you so damned smug.” – Jesse Bowman

“…only the creatively intelligent can prosper in the Lisp world.” – Richard Gabriel

“If someone asks me who I am, and if I’m among ordinary people, I tell them I’m a computer hacker. If I’m among computer people I tell them I’m a Unix hacker. If I’m among Unix people I tell them I’m a Linux hacker. And if I’m among Linux people I tell them I’m a Lisp hacker.” – James Crippen

“Lisp is the red pill.” – John Fraser, comp.lang.lisp

“Lisp is so great … because it is simply the most powerful language available. And the reason everyone doesn’t use it is that programming languages are not merely technologies, but habits of mind as well, and nothing changes slower.” – Paul Graham

“Greenspun’s Tenth Rule of Programming: any sufficiently complicated C or Fortran program contains an ad hoc informally-specified bug-ridden slow implementation of half of Common Lisp.” – Phil Greenspun

“…the greatest single programming language ever designed.” – Alan Kay (about Lisp)

“Lisp is a programmable programming language.” – John Foderaro

“Will write code that writes code that writes code that writes code for money.” – on comp.lang.lisp

“Because Lisp gives you the freedom to define your own operators, you can mold it into just the language you need. If you’re writing a text-editor, you can turn Lisp into a language for writing text-editors. If you’re writing a CAD program, you can turn Lisp into a language for writing CAD programs. And if you’re not sure yet what kind of program you’re writing, it’s a safe bet to write it in Lisp. Whatever kind of program yours turns out to be, Lisp will, during the writing of it, have evolved into a language for writing that kind of program.” – Paul Graham, On Lisp

“Lisp is an especially good language for writing extensible programs because it is itself an extensible program.” – Paul Graham, On Lisp

“Lisp is a language for doing what you’ve been told is impossible.” – Kent Pitman

“Lisp was far more powerful and flexible than any other language of its day; in fact, it is still a better design than most languages of today, twenty-five years later. Lisp freed ITS’s hackers to think in unusual and creative ways. It was a major factor in their successes, and remains one of hackerdom’s favorite languages.” – Eric Raymond, Open Sources

Source: Internet