Week 1

Intro to Lisp

Lisp

Acronym: List Processing || Lots of Irritating Silly Parentheses

# To run lisp progran in VS Code:
> rlwrap sbcl           # starts REPL - read-eval-print loop
* (load "file.lisp")    # assume hello()
* (hello)               # calls hello() method in file.lisp
* (+ 2 3)               # prints 5 (quick temp. form)
* (exit)                # either trash the terminal or enter (exit)

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

    • (1 2 3)
      (a b c d)
      (a "b" T -2.0)
      (a (b c) (d (e f)))
      ()    ; () = NIL
  • 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 to t

  • (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 NIL

      • if 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 ai

    • if all forms but aN evaluate to NIL, or returns evaluation of aN

  • if

    • (if cond then [else])

    • if cond-form is T, then evaluate then-form

    • opt. [else]-form is evaluated if cond-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 names

      • val1, ... 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-1

    analogous to "for i in range(n)" in python

    • syntax : (dotimes (i [n] [completed value]))

      • i - loop iteration variable

      • n - loops until n-1

      • completed value - opt. value that's returned when the loop completes; NIL by default.

  • do : more versatile looping

    • syntax :

      • (do ((i [start value] ([increment by] [operation] i)

      • ([body])))

      • (([exit condition]) i)

      • ([body]))

Note: i needs to be initialized prior.

Last updated