a quick introduction to lisp

Upload: api-26004467

Post on 30-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 A Quick Introduction to Lisp

    1/46

    A Quick Introduction to Lisp

    M. Habibullah Pagarkar

  • 8/14/2019 A Quick Introduction to Lisp

    2/46

    http://xkcd.com/297/

  • 8/14/2019 A Quick Introduction to Lisp

    3/46

    Syntax

    Expression oriented language

    Consists of many expressions or forms

    3 Kinds of expressions

    Literals: 0.42e2, hello, #\c, #(1 2)

    Symbols: a-symbol

    Lists: (f x y)

  • 8/14/2019 A Quick Introduction to Lisp

    4/46

    Numerical Nonsense

    Number

    Real

    Rational Integer

    Fixnum Bignum

    Ratio

    Float Short-float

    Single-float

    Double-float

    Long-float

    Complex

  • 8/14/2019 A Quick Introduction to Lisp

    5/46

    Variables

    Lexical scope

    Variables have no types. Objects do

    Symbols are evaluated as variables

    Bound

    Unbound

    A symbol differs from it's printedrepresentation

    'ooga and 'ooga\ booga

    Interning

  • 8/14/2019 A Quick Introduction to Lisp

    6/46

    Packages

    Namespacing

    Symbol '*package*' stores the currentpackage

    Exporting symbols

    External symbols package:name

    Internal symbols package::name Keyword package

  • 8/14/2019 A Quick Introduction to Lisp

    7/46

    So these are like Ruby'ssymbols, right? Wrong.

    Common Lisp symbols Ruby symbols

    Unique in a package Globally unique

    Can have spaces in a name Can't

    Can be uninterned Can't

    Symbols carry data Used only for identification

    Contain a property list Don't

    Are candidates for GC if unbound Aren't

    Lisp symbols in the keyword package are analogous to Ruby symbols

  • 8/14/2019 A Quick Introduction to Lisp

    8/46

    Cons Cells

    (cons 1 2) (1 . 2)

    (cons 1 '()) '(1)

    From wikipedia

  • 8/14/2019 A Quick Introduction to Lisp

    9/46

    Read-Eval-Print-Loop

    Read form input Lisp object

    Eval input object output object

    Print output object using some sort ofprint representation

    Loop

  • 8/14/2019 A Quick Introduction to Lisp

    10/46

    Evaluation

    Literals are self evaluating

    Symbols are looked up in lexical scope

    Lists

    Function name as first element

    All subsequent symbols are evaluated

    Function called with these params

    Eager evaluation is default behaviour

  • 8/14/2019 A Quick Introduction to Lisp

    11/46

    Preventing Evaluation

    Special form (quote expression)

    Abbreviated as '(expression)

  • 8/14/2019 A Quick Introduction to Lisp

    12/46

    Comments

    ;;;

    ;;

    ;

    #|

    Block comment

    |#

  • 8/14/2019 A Quick Introduction to Lisp

    13/46

    Booleans

    t and nil are constants

    (or exp1 exp2)

    (and exp1 exp2)

    (if test exp1 exp2)

    (cond (test-1 consequent-1)

    (test-n consequent-n))

  • 8/14/2019 A Quick Introduction to Lisp

    14/46

    Equality Excesses

    eq Symbols

    eql Numbers of the same type - default

    equal Lists

    = Best for numbers

    equalp Equality predicate

    string-equals Strings

  • 8/14/2019 A Quick Introduction to Lisp

    15/46

    Regular and Special Functions

    (function hello)

    Or simply #'hello

    (defun hello ()

    Do not mind me. I am a string.

    (print hello))

  • 8/14/2019 A Quick Introduction to Lisp

    16/46

    I wanna know more!

    describe

    type-of

    boundp

    fboundp

    apropos

    documentation

  • 8/14/2019 A Quick Introduction to Lisp

    17/46

    eval ~ apply

  • 8/14/2019 A Quick Introduction to Lisp

    18/46

    Side Effects

    (setf a 5)

    (random 10)

    (defvar b 10)

    (defparameter my-list '(10 20))

    (let ((var-1 val-1)

    (var-n val-n))(body))

    let*

  • 8/14/2019 A Quick Introduction to Lisp

    19/46

    LIStProcessing

    '() nil

    (cons 1 (cons 2 (cons 3 nil)))

    (list 1 2 3)

    '(1 2 3)

    listp, endp, first, second, tenth,rest, car, cdr, cadr, cdddr,member, intersection, nth,nthcdr, append, butlast, reverse,remove etc

  • 8/14/2019 A Quick Introduction to Lisp

    20/46

    List Processing

    assoc, rassoc

    subst

    sublis

  • 8/14/2019 A Quick Introduction to Lisp

    21/46

    Destructive List Surgery

    nconc, nsubst, nreverse, nunion,nintersection, delete

  • 8/14/2019 A Quick Introduction to Lisp

    22/46

    Applicative Programming

    funcall

    map and it's brethren

    lambda

    reduce, every

    find-if, remove-if, remove-if-not,count-if, count-if-not (filter)

  • 8/14/2019 A Quick Introduction to Lisp

    23/46

    Recursion

    Ideas for tail recursive functions

    (defun func (x)

    (cond (end-test end-val)

    (t (func reduced-x))))

    (labels ((fn-1 args-1 body-1)

    (fn-n args-n body-n))body)

  • 8/14/2019 A Quick Introduction to Lisp

    24/46

    IO, IO, off to work we go

    (format t ~s to ~s~% is the timelimit 1 2)

    read

    yes-or-no-p, y-or-n-pwith-open-file

  • 8/14/2019 A Quick Introduction to Lisp

    25/46

    Assignment

    incf, decf, push, pop when, unless

  • 8/14/2019 A Quick Introduction to Lisp

    26/46

    Iteration

    (dotimes (var n (result-form))

    body)

    (dolist (var list (result-form))

    body) return

    (do ((var1 init1 update1)

    (varn initn updaten))

    (test action1 actionn)

    body)

  • 8/14/2019 A Quick Introduction to Lisp

    27/46

    Blocks

    Function bodies are implicit blocks

    return-from

    block

    prog1, prog2, progn

  • 8/14/2019 A Quick Introduction to Lisp

    28/46

    Conditions and Restarts

    break, error, warn, cerror

  • 8/14/2019 A Quick Introduction to Lisp

    29/46

    Function Parameters

    (defun foo (x &optional (y 2) (z5) ())

    &rest

    &key &aux

  • 8/14/2019 A Quick Introduction to Lisp

    30/46

    Simple Objects

    Arrays

    Property Lists

    Structures

    Hash Tables

  • 8/14/2019 A Quick Introduction to Lisp

    31/46

    Macros

    Way to extend Lisp's syntax

    Express concepts concisely

    Expanded at compile time

    It creates an expression that Lisp evals

    (defmacro my-incf (var)

    (list 'setq var (list '+ var 1)))

    (defmacro my-incf (var)

    `(setq ,var (+ 1 ,var)))

  • 8/14/2019 A Quick Introduction to Lisp

    32/46

    Functions Macros

    Arguments are alwaysevaluated

    Arguments are neverevaluated

    Results can be anyvalue/s

    Results can only be avalid s-expression

    Results aren't evaluated Results are evaluated

    What's the difference?

  • 8/14/2019 A Quick Introduction to Lisp

    33/46

    To confuse you a little more

    ` , ,@ &body

    Destructuring

    defvar, defparameter, defconstant

  • 8/14/2019 A Quick Introduction to Lisp

    34/46

    Object Orientation

    Older Object Systems Flavours: Introduced Message passing,

    multiple inheritance & mixins

    New Flavours: Generic functions Common LOOPS

    CLOS

  • 8/14/2019 A Quick Introduction to Lisp

    35/46

    A CLOS Encounter

    Class, instances of classes, genericfunctions and methods

    defclass, make-instance,

    defgeneric, defmethod Class has multiple superclasses, slots

    and a meta-class

    Multiple dispatch Methods defined separately with no

    special access

    Multiple Dispatch & Generic

  • 8/14/2019 A Quick Introduction to Lisp

    36/46

    Multiple Dispatch & GenericFunctions

    Given args, a list of applicable methods isfound

    Find the most specific method

    Apply that method to the args

    Example: (defgeneric f (x y))

    (defmethod f ((x integer) y) 1) (f 1 2.0) 1

    (defmethod f ((x integer) (y real)) 2)

    (f 1 2.0) 2

  • 8/14/2019 A Quick Introduction to Lisp

    37/46

    Lisp and MacGyverLisp and MacGyver

    Yes, these are shamelessly stolen too

  • 8/14/2019 A Quick Introduction to Lisp

    38/46

    What do you mean MacGyver?

    Mid 80s to early 90s Prefer brain over brawn

    Used only simple tools

    Clever solutions to seemingly intractable problems* Entered into popular culture

    Suffered from bad marketing towards the end

    Spin-offs never as good as the original

    Some loyal old-timers still act like keepers of the flame

    * Hey! Wikipedia says so.

  • 8/14/2019 A Quick Introduction to Lisp

    39/46

    http://xkcd.com/224/

    S h h d?

  • 8/14/2019 A Quick Introduction to Lisp

    40/46

    So what happened?

    Worse is betterhttp://www.dreamsongs.com/WIB.html

    H h it ?

  • 8/14/2019 A Quick Introduction to Lisp

    41/46

    Hmm, so who uses it now?

    ITA Software Lisp for High-Performancetransaction processing (Cambridge, MA)

    Cleartrip (Bombay)

    Naughty Dog Software

    Stumpwm

    Movitz

    Oh and this little OS called Emacs

    Wh t b t TDD?

  • 8/14/2019 A Quick Introduction to Lisp

    42/46

    What about TDD?

    LispUnit FiveAM

    Stefil

    LIFT

    cl-unit

    and others

    B k C Li

  • 8/14/2019 A Quick Introduction to Lisp

    43/46

    Books on Common Lisp

    ANSI Common Lisp - Paul Graham Practical Common Lisp - Peter Seibel

    On Lisp - Paul Graham

    Paradigms of AI Programming PeterNorvig

    Lisp Winston and Horn

    B k S h

  • 8/14/2019 A Quick Introduction to Lisp

    44/46

    Books on Scheme

    The Little Schemer series DanielFriedman and Matthias Felleisen

    SICP - Gerald Sussman and Harold

    Abelson The Scheme Programming Language

    Kent Dybvig

    Teach Yourself Scheme in Fixnum Days Dorai Sitaram

    Upcoming Books

  • 8/14/2019 A Quick Introduction to Lisp

    45/46

    Upcoming Books

    Land of Lisp Conrad Barski (2010)www.lisperati.com

    Lisp Outside the Box Nick Levine (2010)

    Which Lisp?

  • 8/14/2019 A Quick Introduction to Lisp

    46/46

    Which Lisp?

    Linux SBCL* Mac OS X Clozure CL*

    Windows Corman CL, Allegro CL,

    LispWorks, CLISP* Or try Scheme/Clojure

    Try to accept Emacs :)

    http://gigamonkeys.com/lispbox/

    http://common-lisp.net/~dlw/LispSurvey.html

    * Open Source

    http://common-lisp.net/~dlw/LispSurvey.htmlhttp://common-lisp.net/~dlw/LispSurvey.html