Dana Vrajitoru
C311 Programming Languages
Lisp Variables, Functions, Conditional
Values, Variables
- Comments: anything starting with ; till the end of the line.
- Constants like numbers or strings are evaluated to their value:
81, "hello", 'a
- Special values : nil meaning false, and t
meaning true.
- Variables: defined by an identifier and containing a value. We can
attach a description to them.
- Variables are usually global (unless specified otherwise).
Defining Variables
- Assigning values to them:
(setq var1 val1 var2 val2 ...)
- Defining global variables with description (elisp):
(defvar name value description)
- Local variables:
(let ((var1 val1) (var2 val2) ... )
sequence of expressions)
Expressions
- An expression in the form of a list is evaluated by interpreting
the first symbol as a function and the rest of the list as
arguments.
(+ 1 2 3 4) ; => 10
(* 2 6) ; => 12
(concat "hello " "world") ;=> "hello world"
- Each argument can also be an expression.
Evaluation in Lisp
- A constant is evaluated to itself.
- An identifier by itself is interpreted as a variable and evaluated
to its value.
- An expression preceded by a quote is returned without evaluation.
- A sequence of expressions is evaluated by evaluating each
expression in order, and the returned value is the result of the last
expression in the sequence.
- A function invocation (call) is evaluated by evaluating each
parameter first, then by calling the function with the resulting
values.
Defining Functions
(defun name (arg1 arg2 ...)
"function description"
sequence of expressions)
The function description is optional and specific to elisp.
(defun square (x)
(* x x)) ; => square
(square 5) ; => 25
Conditional
(if (condition)
expression if true
expressions if false)
Alternatives in elisp for the conditional:
(when (condition) expressions)
(unless (condition) expressions)
(defun max (x y)
(if (> x y) x y))
Example - Factorial
factorial(n) = n * factorial(n-1)
(defun factorial (n)
"Computes the factorial of a number"
(if (<= n 1) 1
(* n (factorial (- n 1)))))
(factorial 5) ; 120
List Length
Example - sumlist
(defun sumlist (L)
"Returns the sum of the elements of the list."
(if (not L)
0
(+ (car L)
(sumlist (cdr L)))))
(sumlist '(8 2 5 1)) ; 16
Function Example
(defun last (L)
"The last element in a list"
(if (not L)
nil
(if (not (cdr L))
(car L)
(last (cdr L)))))
(last ()) ;-> nil
(last '(1)) ;-> 1
(last '(1 2 3 4)) ;-> 4
Alternative version, considering that when the list is empty both the
car and the cdr return nil:
(defun last (L)
"The last element in a list"
(if (not (cdr L))
(car L)
(last (cdr L))))
Grouping Expressions
- Specific to elisp
(progn expr1 expr2 ...)
- Evaluating each of the expressions in the list. It is useful when
the syntax requires only one expression (like for the conditional).
- The value returned by this expression is the value to which is
evaluated the last expression in the list.
Complex Conditional
(cond (c1 exp1) (c2 exp2)... )
- Evaluates each condition until one of them is true.
- When that happens, it evaluates the expression(s) associated with
it.
- If none of them is true, it returns nil.
- Each of the cases can contain a sequence of expressions (not just
one).
- Equivalent to a sequence of nested simple conditionals or a
sequence of if - elif - else.
Example
(progn
(setq n (% (random) 100))
(cond
((< n 0)
(princ "you are not born yet")
(princ "what are you doing here?"))
((< n 15) (princ "you are in junior high"))
((< n 16) (princ "you'll learn to drive soon"))
((< n 21) (princ "you shouldn't be drinking"))
(t (princ "believe it or not, you're an adult now")))
)
Examples
(defun last (L)
(cond ((not L) nil)
((not (cdr L)) (car L))
(t (last (cdr L)))))
(defun max3 (n1 n2 n3)
(cond
((and (>= n1 n2) (>= n1 n3)) n1)
((and (>= n2 n1) (>= n2 n3)) n2)
((and (>= n3 n1) (>= n3 n2)) n3)))
Atoms, Pairs and Lists
- Atoms in Lisp are indivisible expressions: numbers, constant
strings, symbols, variables.
- An empty list is both an atom and a list.
- Fundamental construction: dotted pair or cons:
'(2 . 3) equivalent to (cons 2 3)
- A list by definition is a dotted pair of an expression and a list
(which can be empty).
- A list with one element:
'(a . ()) or '(a) or (cons 'a ())
(cons 1 (cons 2 (cons 3 ()))) ; => '(1 2 3)
List Manipulation
- Fundamental list functions: car, cdr
- car - equivalent to the "datum" in a C++ list.
- cdr - equivalent to the "next" in a C++ list.
(car '(1 2 3)) ; => 1
(cdr '(1 2 3)) ; => (2 3)
- Second element in the list:
(car (cdr '(1 2 3))) ; => 2
- Simple list creation:
(list 'a 'b 'c) ; => (a b c)
Other List Operations
Lisp Built-in Names
- cons - short for construct
- car - short for 'Contents of the Address part of the Register'
- cdr - short for 'Contents of the Decrement part of the Register'
- cadr - Contents of the Address part of the Decrement part of the Register.
(car (cdr ...))