- Rationale: too many parentheses in s-exp, list notation simplify the format
- Definition: A list is a sequence of either atoms or other lists separated by blanks and enclosed in parentheses.
- Can be used to create expressions and data structures.
e.g. Representing 40, 10, 50, 20, 30.
- s-exp:
- list-notation:
(40 10 50 20 30) = (40 . (10 . (50 . (20 . (30 . NIL)))))
** list notation is essentially a linked list
Format
- Can have 0 or more element, i.e. empty list; NIL = () -- note that an empty list is both an atom and a list.
- List items can be atoms, non-atoms, or even lists,
e.g. ((40 . 10) (50 . 20) 30) or ((40 10) (50 . 20) (70 60) 30) are both legal
Functions
- Written in a list notation
- First item of the list is the function name
- Remaining items of the list are function params
e.g. (f x y) in lisp is equivalent to f(x,y) in other languages
read-eval-print
- Lisp read list from user input
- Evaluate the function with the arguments
- Evaluate arguments
- Apply result to function
- Do it recursively until all functions evaluated
- Print the result to the interpreter console
- Loop back to step 1
Math expressions are also examples of functions:
Math Expressions
(* 7 9) = 7 x 9 = 63
(– (+ 3 4) 7) = 3+4-7 = 0
read-eval-print
- Lisp read expressions from user input
- Evaluate the expression
- Print the result to the interpreter console
- Loop back to step 1
Lisp evaluates everything
- An expression -- return expression result
- Number (e.g. 5) -- return the number, i.e. 5
- Bounded symbol (x) -- return value bounded to that symbol
- Unbounded symbol (x) -- return error, because the symbol cannot be evaluated
Native functions
- list: takes any number of arguments and constructs a list of those elements
- nth: takes a number and a list as arguments and return the indicated element of the list
> (list 1 2 3 4 5)
(1 2 3 4 5)
> (nth 2 (list 1 2 3 4 5))
3