Week 1
Intro to Lisp
Lisp
Acronym: List Processing || Lots of Irritating Silly Parentheses
Syntax Basics
Atoms
: basic syntactic units of the language ex:
Numbers
Integers (
0, -1, 69
)Ratios (
1/2, 2/3, 4/2
)Floats (
0.0, -1.0, +1.5
)
Individual Chars :
#\a, #\@
Strings :
"string"
Boolean :
T (true), NIL (false)
Symbols - names (made up of strings/numbers/chars) used for naming functions, variables, & other entities:
2021CPS305, symbolName123, +, T, NIL, <>, -+49ers+-, <+==+>
Lists : sequence of either atoms or other lists separated by blanks
Vectors : faster list-like sequence
#(1 2 3)
,#(a "b" -2.0)
S-Expressions
symbolic expressions (aka function application: non-empty list are themselves forms)
syntax :
( op a1 a2 a3 ... )
op
: function || operator
a1 a2 a3 ...
: arguments || operands
Forms
: lisp expressions that may be meaningfully evaluated
(= (+ 2 3) 5)
: evaluates tot
(a b c)
: ERROR 'a' is not a function (this is a list, not a form)
Variables & Constants
below ex. are for global vars/const ; for local vars use
let
>defvar
Variables
DEFINE =
(defvar varName varValue "var desc.")
SET =
(setq varName newValue)
Constants
DEFINE =
(defparameter constName constValue)
SET =can't change const value
Named Procedures (aka Functions)
syntax : (defun [funcName] ([parameters]) ([body]) )
Lambda (Anonymous Functions)
much like in other languages,
lambda
fx's are quick one liners that are defined and called in the same line.
syntax : ( (lambda params body) arguments )
Blocks + Progn
Block
Blocks allow for contorlled sequential execution.
syntax :
(block blockName body)
Progn
skipped the blockname and can call the block directly
syntax :
(progn body)
Special Forms
If op
is 1 of 25 special words, the form is evaluated as a special form
Special Forms usually produce values (but not always)
quote
(quote v)
aka'v
where v=form : returns v as data, w/o evaluating it.
and / or
The following objects are interpreted as NIL :
()
,'()
,NIL
,'NIL
All other objects are interpreted as True
and a1 ... aN
all forms a1...aN are evaluated (L-to-R)
if any form evaluates to NIL,
and
returns NILif none of the forms are NIL,
and
returns evaluation of aN
or a1 ... aN
all forms a1...aN are evaluated (L-to-R)
if any form ai evaluates to true,
or
returns evaluation of aiif all forms but aN evaluate to NIL,
or
returns evaluation of aN
if
(if cond then [else])
if
cond
-form is T, then evaluatethen
-formopt.
[else]
-form is evaluated ifcond
-form is NIL (false)
When we want to evaluate several conditions in a row we use
cond
:let
special form for temporary local variable binding
syntax :
(let ((var1 val1) … (varN valN)) <s-expressions>)
var1, ... varN
: variable namesval1, ... valN
: initial values assigned to respective vars (default=NIL)each varN is assigned to their valN, and finally the s-expression is evaluated.
The value of the last expression evaluated is returned
Loops
dotimes
: used to create simple loops that loop from 0 to n-1analogous to "for i in range(n)" in python
syntax :
(dotimes (i [n] [completed value]))
i
- loop iteration variablen
- loops until n-1completed value
- opt. value that's returned when the loop completes;NIL
by default.
do
: more versatile loopingsyntax :
(
do
((i
[start value]
([increment by]
[operation]
i
)(
[body]
)))((
[exit condition]
)i
)(
[body]
))
Note:
i
needs to be initialized prior.
Last updated