Lisp
Summary #
is a family of programming languages with fully parenthesized prefix notation. LISP: LISt Processor lispcookbook
Prefix Notation #
LISP uses what is called a prefix notation, i.e. instead of writing 10+20 (this is called the infix notation) you have to write + 10 20.
S-expressions #
In the usual parenthesized syntax of Lisp, an S-expression is classically defined[1] as
- an atom, or
- an of the form (x . y) where x and y are S-expressions.
Data-types #
Two fundamental data types: #
- Atoms
- Lists
e.g: (1 2 foo) is list with atoms 1, 2 and foo
Quote or “’” or apostrophe #
Difference is applying function to symbol(quote) vs to value.
(atom? ’turkey) #
“atom?” is a function and “’turkey” is the argument
(atom? 'turkey) ;; is same as
(atom? (quote turkey))
;; in the above s-expression
;; atom? gets evaluated to a function and "turkey" to a symbol not function.
;; so the function is applied to the symbol
(atom? turkey) #
(atom? turkey)
;; atom? evaluates to function
;; turkey is variable, evaluating turkey gives the value that is bound to it.
;; function is applied to the value
Dictionary or hastable #
http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node154.html
(setq a (make-hash-table))
(setf (gethash 'color a) 'brown)
(setf (gethash 'name a) 'fred)
(gethash 'color a) => brown
(gethash 'name a) => fred
(gethash 'pointy a) => nil
4 ways to implement dictionary #
stackoverflow Common Lisp has at least four different ways to do that (key value storage):
property lists (:foo 1 :bar 2) #
assoc lists ((:foo . 1) (:bar . 2)) #
hash tables #
CLOS objects #
CLOS objects (slot-value foo ‘bar) to get and (setf (slot-value foo ‘bar) 42) to set. The slot name can be stored in a variable: (let ((name ‘bar)) (slot-value foo name)) .