Lisp Sam Bassiouni Joshua Linge Chris Anderson Matthew

Lisp Sam Bassiouni, Joshua Linge, Chris Anderson, Matthew Countryman

John Mc. Carthy • • Born: 1927 Died: 2011 Turing Award, Kyoto Prize National Medal of Science, Benjamin Franklin Medal Invented Term "Artificial Intelligence" and heavily contributed to the early field Developed Lisp programming language family Influenced design of the ALGOL language First made popular time sharing concepts

Lisp History • • Invented by John Mc. Carthy in 1958 at MIT S - expression based - defined inductively an atom o (x y) where x and y are S-expressions o • • First implemented on IBM 704 by Steve Russell who realized eval could be implemented in machine code Lisp dialects use car and cdr car - first item in the list o cdr - the rest of the list o

Lisp History - Cont'd • First complete compiler 1962 Tim Hart, Mike Levin - MIT o Introduced incremental compilation § Allows the use of both compiled and interpreted lisp code at the same time o • • • New dialects are generated over the next 25 years Common Lisp introduced to "unify" the existing Lisp dialects Scheme and ISLISP also very popular

Timeline of Lisp 1955 Lisp 1. 5 Maclisp Zeta. Lisp NIL Interlisp Common Lisp Scheme ISLISP 1960 1965 1970 1975 1980 1985 1990 2005 2012

Uses • Artificial Intelligence Lisp is very powerful with regards to its ability to hand symbolic expressions. o Very easy to prototype different problems and tackle unsolved problems with o • Visual Rendering Software (CAD) Very fast interpreted symbolic language on today's hardware o Symbolic notation abstracts more complicated data path problems which exist in these kinds of applicants o

Lisp Contributions • • Automatic memory management o Mark-and-sweep algorithm Dynamic typing o No type checking at compile time Tree data structures Self-hosting compiler

Common Lisp Syntax • • Expressions are defined by open and closed parentheses. (+ 2 2) An expression does not distinguish between code and data. Each expression evaluates to a value or multiple values. Expressions in Lisp are called Symbolicexpressions (S-Expressions).

S-Expressions consist of two basic elements • • Lists – Elements understood by the interpreter within parentheses separated by whitespace. (1 2 3) ((a b) (1 2)) (+ 2 5) Atoms – Everything else. o Common forms of Atoms are: § Numbers § Strings § Names

Using Expressions • • Expressions are written as lists, using prefix notation. The first element in the list is the name of the function or operator and the remaining elements are arguments. Lisp originally had very few control structures, but many more have been added over time. Lisp's original conditional operator, cond, is the precursor to later if-then-else

Lists are represented as singly linked lists • Each cell in the linked list is called a "cons" and contains two pointers called "car" and "cdr". car points to the data. o cdr points to the next cons. o

List-Processing Functions • • • The car function returns the first element as an atom. (car 9 8 7) evaluates to 9 The cdr function returns the rest of the list. (cdr 9 8 7) evaluates to (8 7) The cadr function returns the second item in the list. (cadr 9 8 7) evaluates to 8

List-Processing Functions - Cont'd • • • The list function creates a list with the given arguments. (list 2 3 5) evaluates to (2 3 5) The cons function adds an element to the front of the list. (cons (1) (2 3 5)) evaluates to ((1) 2 3 5) The append function appends two or more lists to one another. (append (1 2) (3 4)) evaluates to (1 2 3 4)

Arithmetic Operations • The first element is the specific operation and the arguments follow. (+ 1 2 3 4) (* 2 3) (+ 2 (/ 7 3))

Relational Operators (= x y) (/= x y) (< x y) (> x y) (<= x y) y (>= x y) to y Is Is Is x x x equal to y not equal to y less than y greater than y less than or equal to Is x greater than or equal

True and False • • T - True, non-nil evaluates to T. nil - Empty list, also False. atom - Returns T if an atom is passed in, nil otherwise. null - Returns T if the input is an empty list. numberp - Returns T if an atom is a number. eq - Takes two arguments and returns T if both are the same atoms. equal - Takes two arguments and returns if they are equal. Works with lists.

Conditional Functions • • • if - 3 arguments: condition, if true, if false. (if t 5 6) evaluates to 5 (if nil 5 6) evaluates to 6 when - 2 arguments: condition, if true. (when t 3) evaluates to 3 (when nil 3) evaluates to nil unless - 2 arguments: condition, if false. (unless t 3) evaluates to nil (unless nil 3) evaluates to 3

Conditional Functions - Cont'd • cond - Allows any number of clauses made of a conditional followed by actions. If the conditional is nil, move to the next conditional. If it is T, evaluate following actions and return. (cond ((evenp a) a) ((> a 7) (/ a 2)) ((< a 5) (- a 1)) (t 17)) • ; if a is even return a ; else if a is bigger than 7 return a/2 ; else if a is smaller than 5 return a-1 ; else return 17 case - Like a switch. "otherwise" is a catch-all. (case x (a 5) ((d e) 7) f) 3) (otherwise 9)) ; if x is a return 5 ; if x is d or e return 7 ((b ; if x is b or f return 3 ; otherwise return 9

Iterative Functions • • loop – Repeatedly executes its body until a 'return' is hit. (loop (setq a (+ a 1)) (when (> a 7) (return a))) dolist – Binds each item in a list to a variable and executes with it until end of list. returns nil. (dolist (x '(a b c)) (print x))

Iterative Functions - Cont'd • do – first: variables with initial values and update procedure, second: termination condition and return value, third: body (do ((x 1 (+ x 1)) (y 1 (* y 2))) ((> x 5) y) (print 'working) )

Functions • • defun – Defines a function. Takes function name, args, and function definition (defun square (x) (* x x)) defmacro – Defines a macro. Unlike a function, doesn't evaluate arguments defstruct – Defines a struct. Similar to C structs lambda – Anonymous function (defun plusone (L) (mapcar #'(lambda (x) (+ x 1)) L))

Functions - Cont'd • • • setq – globally binds a variable (setq x 5) let – locally binds a variable to a block prog – allows local vars and executes each statement in body

Functions - Cont'd • • • funcall - calls the first argument on the remaining arguments mapcar - takes a function and a list, passes each item in list into function and cons results with nil mapcan - same as mapcar, but uses append instead of cons sort - takes a list and a sorting function, returns sorted list gensym - generates random variable name

Other Symbols • • ; (comment) – everything past a semicolon is a comment until the end of the line ' (quote) – prevents argument from being evaluated. first item in a list is evaluated (assumed to be a function) unless a ' is used ` (backquote) – same as regular quote but allows , (comma) to be used to evaluate #' (function) – argument is interpreted instead of evaluated

Argument Options • • • &rest – collects extra arguments and binds them to a list &optional – arguments after which are optional when called &key – arguments can be passed in any order if labeled

Code Examples
- Slides: 26