Tuesday, August 26, 2014

Read-eval-print loop

The lisp interpreter works in a Read-eval-print loop or REPL for short. To understand this we can imagine that the lisp interpreter reads the first word till whitespace, interprets it, reads more words if required , interprets them and prints the output.
It can also be understood as (function_name argument1 argument2 .....) where each argument can be a function in itself. So to print a string all is required is the string in double quotes.

CL-USER> "hello, world"
"hello, world"


Adding two numbers becomes more complicated. But it is easier if you remember that the function/operator comes first followed by the arguments. So,

CL-USER> (+ 2 3)
5


Here + is the operator followed two arguments, 2 and 3. So the lisp interpreter knows after reading the first word that it should expect numbers and the user expects to add all these numbers.  So if we specify 10 numbers then we can get their sum in one line.

CL-USER> (+ 1 2 3 4 5 6 7 8 9 10)
55

But what if we do this with subtract?

CL-USER> (- 3 2 1)
0


and divide...

CL-USER> (/ 1 2 4)
1/8

More functions tomorrow...

Monday, August 25, 2014

First day

I am beginning to learn lisp and I thought I would document the whole process of learning in a blog. The environment that I plan to learn to learn lisp is the following. There is no particular reason for choosing any of these tools.

  • Ubuntu 14 - quite comfortable with the OS.
  • SBCL (Steel Bank Common Lisp) - Goal is to learn this.
  • Emacs - Have never used it before. I am a Vim fan.
  • Slime - An IDE to make things easier.
Installation steps:

  1. C-M-t to open terminal
    sudo apt-get install emacs24
    sudo apt-get install sbcl
    sudo apt-get install slime
    curl -O http://beta.quicklisp.org/quicklisp.lisp
  2. quicklisp installation steps here.
  3. Run emacs
  4. C-x C-f test.lisp
  5. C-x 3
  6. M-x slime
  7. In slime window type
    1. (+ 1 2)↵
    2. Check that result is 3
  8. In other window type
    (defun test(x y)
        (+ x y))
  9. C-c C-c
  10. Slime window
    1. (test 3 4)↵
    2. Check that output is 7.