As of today, I have never seen any LISP programming language before. I am writing this article while I am learning LISP, why am I doing this? It is because I made a joke on a tweet about JavaScript (ECMAScript 2015) keywords.
On Twitter, @stephenjohnlimb replied and suggested LISP avoid those JavaScript keywords. I told him that I am going to try LISP programming tomorrow, so that's why I am here writing about the experience.
Getting Started
Right now, I'm at google.com looking for a LISP programming resource. @mohjb said that Lisp was the 2nd language (after assembly). Oh boy!
I have been googling around and trying another bunch of stuff and then finally, I found this simple site to learn LISP!
Installing Common Lisp
I am on my M1 MacBook Pro and already have brew.sh installed. HomeBrew is awesome because it makes my life easier installing programs to run in the terminal.
Inside the terminal, I only have to type in: brew install sbcl
and then bang! It's installed! Also, we have to install a package manager for LISP which is called quickLISP.
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
Creating Hello World in LISP
I am going to read the tutorial and my goal is to print out Hello World
from LISP! so I can start somewhere here. I'm opening up the VS Code app to start writing my first Hello World on LISP!
;;; helloworld.lisp
;;; by Paul Sweeney Jr
;;;
;; This is a comment
;;
(format t "Hello, world!") ; this is inline comment
I am saving this as helloworld.lisp
Running LISP
I am not sure, what's next? So, I just type in man sbcl
to read the manual on the terminal. I figured out that I can type in sbcl --script helloworld.lisp
to run my very first LISP program!
Format string
I want to do some math for my LISP program. So, I googled format lisp
to find out how to print out numbers. I found the answer and it is (format t "~d" 3)
to output a number. You can dive into wiki about format text on LISP
Let do some math
Lisp does have some math functions to use. Those are a few basic examples here.
(+ 3 6) ; 9
(- 1 5) ; -4
(* 3 3) ; 9
(/ 10 2) ; 5
This is where I learned about math at this website: www2.cs.sfu.ca
Adding defined functions
I am adding my own defined function to my LISP program!
;; functions
;;
(defun add (x y)
(format t "~d" (+ x y))
)
(defun sub (x y)
(format t "~d" (- x y))
)
(defun multiply (x y)
(format t "~d" (* x y))
)
(defun divide (x y)
(format t "~d" (/ x y))
)
(add 1 2)
(sub 1 4)
(multiply 3 3)
(divide 10 2)
Result
I ran the program and here is the result but it's not what I am expecting.
Hello, world!3-395%
I googled on how to add linefeed to my program... here is it! (format t "~d ~%")
and whoa! I did it and it works after some struggling.
(format t "~d + ~d = ~d ~%" (values x) (values y) (+ x y))
Final Program
Final Result
vphreak@Pauls-MBP lisp % sbcl --script helloworld.lisp
Hello, world!
1 + 2 = 3
1 - 4 = -3
3 * 3 = 9
10 / 2 = 5
Wrapping up
So far, I learned how to run LISP program on my computer and output Hello, World!
, also added some defined functions with some math. I will come back to LISP someday if I want to learn more.