15
ELISABETH CHILDS COMMON LISP

COMMON LISP - Colby College · • Common LISP • Supports procedural, functional, and object oriented programming paradigms • Scheme – smaller, less built in features, no macros

  • Upload
    others

  • View
    31

  • Download
    0

Embed Size (px)

Citation preview

E L I S A B E T H C H I L D S

COMMON LISP

BRIEF HISTORY

•  Developed in 1958 as a functional language •  Common LISP •  Supports procedural, functional, and object oriented

programming paradigms

•  Scheme – smaller, less built in features, no macros

•  Pioneer of many ideas in CS •  Garbage collection •  Dynamic Typing

•  The name derives from “LISt Proccessing” •  Linked lists, main data structure

S-EXPRESSIONS

• Lists •  Surrounded by

parenthesis •  Made up of lists and

atoms •  LISP lists start with a

Symbol

• Atoms •  Everything that isn’t a

list AKA Symbols •  Variables •  Functions names •  T and nil – the truth

values in LISP •  Keyword •  Symbols •  Numbers •  Strings •  Chars

(+ (* 6 8) 5)

Atom

Lists

BASIC LAYOUT OF FUNCTIONS

•  (function-name parameters)!•  (+ 3 4 5 6) •  + is the function name •  3 4 5 6 are the parameters for the function

•  (defvar *owl* 1) •  defvar – function name •  *owl* - symbol •  1 – the value to set to the symbol

LISTS

•  Cons – Node •  Car – Node’s value Synonym: first •  Cdr – Node’s next pointer Synonym: rest

Cons

Cdr

Car

NAMES

•  Lisp is not a case sensitive language •  owl == Owl == OWL •  CamelCase is not used much in list •  - is used most frequently to separate words in lisp

•  Special Characters in Lisp •  ( ) “ ‘ ` , ; \ - you can still include them in names •  Valid names

•  owls-can-fly à OWLS-CAN-FLY ;a regular name •  Owls\ can\(fly\) à |OWLS CAN(FLY)| ; escaping characters •  O|wls can |fly à |Owls can FLY| ; using the || to escape list of chars •  1guffo à 1GUFFO ; Numbers at the beginning of a name •  14.8x3 à |14.8X3| ; A name that looks like a number •  8+9 à |8+9| ; Syntax with meaning in other languages

•  Invalid names •  A name of all periods •  Anything that can actually evaluate to a number

•  Global Variables *OWL* •  Constants +OWL+

NUMBERS

•  The syntax matters in identifying the type of a number •  Integers – can be arbitrarily large •  -313               Negative •  313               Positive •  626/2             Another representation •  #b0101 Binary 5 •  #x1AF Hex 431

•  Ratios •  1/2                 The ratio one over two •  (+ 1/4 1/8) à 3/8

•  Floating Point •  3.0            Single precision floating point •  3.0e2 Single precision 300 •  3.0d0 double point precision

•  Complex numbers •  #c(real imaginary) à both the real and imaginary are ratios or floating

point

FUNCTIONS FOR NUMBERS

•  + - * / •  floor - rounds to negative infinity •  mod – is % in other language •  Conditionals •  ( = number number) – Equivalent to == •  (/= number number) – Equivalent to != •  (< number number) – Equivalent to less than •  (<= number number) – Equivalent to less than or equal to

•  ++, --, +=, -= à (incf x) (decf x) (incf x 10) (decf x 10) •  and and or are there own functions

ASSIGNMENT

•  (defvar owl 10)!•  (defparameter owl 10)!•  (let ((owl 5) (fly “I can Fly”)) …)!•  Reassigning!•  (setf owl 5) !

SCOPE

•  Functions create there own scope

FUNCTIONS

FUNCTION AS ARGUMENTS

PARAMETERS

&optional

&rest

&key

MACROS

•  Are used to add functionality and syntax to the language. •  A function to add functionality directly to the

language •  defvar is an example of a macro it returns actual

lisp code that can be called and run. •  Its pretty cool