CS 3723
 Programming Languages 
   Main Lisp Page  


Overview: This is the Lisp Introduction Page. Refer to various Lisp materials online through the calendar page, or at the links below:


Basics of Lisp
  • Simple syntax: The Lisp interpreter reads a sequence of S-expressions.

      An S-expressions is either:
      • an atom (a constant or identifier), or
      • a parenthesized list of S-expressions, separated by blanks when necessary (and not by commas).

    Notice the recursive form of this definition, showing from the start how important recursion is to Lisp. Lisp S-expressions always return a result, and they also often have side-effects, such as defining or invoking functions.

  • Constants: The simplest form of S-expression is a constant, which has itself as value. Lisp is not case-sensitive, but our input to the Lisp interpreter will be in lower-case, and Lisp will change it to upper-case, except for letters inside double quotes.

    Atoms
    > 47                 ;;; integer constant
    47
    > "Lisp"             ;;; string constant
    "Lisp"
    > -.34               ;;; float constant
    -0.34
    > -.34e-2            ;;; float constant
    -0.0034
    > 3.141592653589     ;;; float constant
    3.1415927
    > 3.141592653589d0   ;;; double constant
    3.141592653589d0
    > 666d0              ;;; double constant
    666.0d0
    > t                  ;;; this is true in Lisp
    T
    > nil                ;;; this is false
    NIL
    > ()                ;;; this is also false
    NIL
    > (equal () nil)    ;;; they are equal!
    T
    

  • Simple lists: Given a simple list of atoms, Lisp regards the first item as a function, makes a list out of the remaining arguments, and then applies this function to that list of arguments:

    Evaluating Simple Lists
    > (+ 8 5)
    13
    > (/ 355 113)
    355/113
    > (/ 355.0 113.0)
    3.141593
    > (/ 355d0 113d0) ; "d0" for "double"
    3.1415929203539825d0
    

  • Evaluating lists: The previous section didn't tell the whole story. When Lisp is given a list, it evaluates the list (using Lisp function eval). Lisp takes the first (top-level) element of the list. It must be an atom that represents a function. Then Lisp evaluates each of the other elements of the list recursively (again using eval). Lisp makes a list of these evaluated elements, and applies the first function to these values. Remember: In a simple list being evaluated, the first item must be the function, and arguments come after.

    Evaluating Complex Lists
    > (* (+ (* 5 4) (/ -4 2) ) 37);((5*4)+(-4/2)*37=18*37
    666
    > (* 2 (asin 1))  ; float: asin(1) = pi/2
    3.1415927
    > (* 2 (asin 1D0))  ; double
    3.141592653589793d0
    > (* 2 (asin 1L0))  ; long double
    3.1415926535897932385L0
    > (+ (* 2 2) (* 3 3) (* 5 5) (* 7 7) (* 11 11)  
        (* 13 13) (* 17 17)) ; last ')' completes S-expr
    666   (Sum of squares of first 7 primes = 666)
    


Lisp Workpages: These contain problems needed for Recitations 9 and 10.

  • 1: Simple Computations   (basically just sequences of assignments, mostly numberical).
  • 2: Conditionals and Functions   (focus on recursive functions, using cond and defun, again mostly numberical).
  • 3: Lists in Lisp   (focus on recursive processing of lists, using car, cdr, cons, list, and append, mostly non-numberical programming; mention mapcar and apply at the end).


Example Functions: Here are several additional examples, all working with lists: functions.


Internal representations and dotted pairs:


Use of mapcar and apply:


Revision date: 2013-10-26. (Please use ISO 8601, the International Standard Date and Time Notation.)