Getting Started with Lisp
- By : Mydatahack
- Category : Infrastructure, Opinions
- Tags: Lisp
There are good reasons why you want to learn Lisp. As Paul Graham said, the reason why you want to learn it is the same as the reason why you want to learn Latin. You probably won’t use Latin. But, the knowledge will enrich your experience with the language you use daily. You don’t need to learn Lisp to write the next big app or get your dream programming job, but knowing it will make you a better programmer.
Here are some inspirations you need to get started with Lisp.
- Beating the averages by Paul Graham
- How To Become a Hacker by Eric Steven Raymond
Ok, now you are inspired and want to learn Lisp. Then, the problem is that Lisp has dialects. We need to choose one.
Lisp these days has three major dialects, Common Lisp, Scheme, and Clojure. They are similar and different. Here are some of the important differences that you may want to consider when you choose which one. At the end of the day, it really doesn’t matter which one you pick unless you need to work on a project that uses one of them. If you are learning it to broaden your programming knowledge, any of them is fine. In this post, I chose Common Lisp because I don’t want to have anything to do with JVM and it seems to be used in a real world scenario more.
Common Lisp
– Paradigm agnostic (both functional and OOP).
– Used more in real world problems.
Scheme
– Similar to Common Lisp.
– Used more in an academic context, like computer science and programming language research.
Clojure
– Favours functional programming.
– Runs on JVM (this is good and bad…)
Get started with Common Lisp
(1) Install SBCL
Use command line for Linux and Mac. Use LispStick for Windows. SBCL (Steel Bank Common Lisp) is a high performance Common Lisp compiler. It provides an interactive environment and runs Lisp through the command line.
# mac brew install sbcl # linux sudo apt-get install sbcl ```
(2) Install Quicklisp (package manager)
curl -o /tmp/ql.lisp http://beta.quicklisp.org/quicklisp.lisp sbcl --no-sysinit --no-userinit --load /tmp/ql.lisp \ --eval '(quicklisp-quickstart:install :path "~/.quicklisp")' \ --eval '(ql:add-to-init-file)' \ --quit ```
(3) Run it in an interactive mode
We can just type `sbcl` in the bash. Then run whatever. Here is the hello world!
(format t "Hello World")
You can exit from REPL by calling quit.
(quit)
(4) Run it from a file
Create a file with .lisp. For an IDE, VS code with lips extension will be sufficient to start with.
fib.lisp
(defun fib(n) "Retun the nth Fibonaccie number." (if (< n 2) n (+ (fib ( - n 1)) (fib (- n 2)) ) ) )
Then, run the function with `sbcl`
sbcl --load fib.lisp
Then, run the function in the console.
(fib 30)
(5) Compiling Lisp
Lisp is both compiled and interpreted language. We can run it interpreted or compiled.
Start the REPL and this will run the function in an interpreted way.
(load "fib.lisp")
This will compile the file in the same folder. It will create `fib.fasl` file.
(load (compile-file "fib.lisp"))
Once you have the file, you can load the compiled file.
sbcl --load fib.fasl
Ok, that’s the basic. This will give you enough to get started with Lisp!