Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Common Lisp: A Short Introduction on Most Inter...

unionx
November 13, 2012

Common Lisp: A Short Introduction on Most Interesting Parts

伞哥@田春冰河 做的一个关于Common Lisp的介绍

unionx

November 13, 2012
Tweet

More Decks by unionx

Other Decks in Programming

Transcript

  1. 1 Common LISP An short introduction on most interesting parts

    On Principles of Programming Languages Chun Tian (binghe)
  2. 2 CONTENTS HISTORY The History of LISP. . . .

    . . . . . . . . . . . . . . . . . . . . 3 LANGUAGE OVERVIEW LISP Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Common Lisp Features . . . . . . . . . . . . . . . . . . . . 4 EXAMPLE: A FACTORIAL FUNCTION PROGRAMMING PARADIGMS Multi-Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . 6 DATA TYPES IN COMMON LISP Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Forms and Functions . . . . . . . . . . . . . . . . . . . . . . 8 PROGRAM STRUCTURE LIST PROCESSING Functions on Lists . . . . . . . . . . . . . . . . . . . . . . . . 9 Map/Reduce: a sample use . . . . . . . . . . . . . . . . . 10 SEQUENCE OPERATIONS The CLOS Approaches . . . . . . . . . . . . . . . . . . . . 11 COMMON LISP OBJECT SYSTEM CLOS, PART I COMMON LISP OBJECT SYSTEM CLOS, PART II COMMON LISP CONDITION SYSTEM MAINTENANCE OF LISP IMAGE RUNTIME SYSTEM CL IMPLEMENTATIONS Common Lisp Implementations (not all!) . . . . . . . . 16 REFERENCES Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Papers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
  3. 3 The History of LISP . LISP 1 on IBM

    704: 1958-1959 . Lisp 1.5 to PDP-6 Lisp: 1960-1965 . MacLisp and InterLisp (late 1960’s - early 1980’s) . The Early 1970’s: various Lisp systems . Lisp Machines: 1973-1980 . Scheme: 1975-1980 . Prelude to Common Lisp (CLTL1): 1980-1984 . Standards Development (CLTL2): 1984-1990 . Modern commercial platforms: 1987-1999 . Modern open source platforms: 1999-2009 . QuickLisp and 3rd-party libraries: 2009-2012 HISTORY FORTRAN = FORmula TRANslator LISP = LISt Process
  4. 4 LISP Features . Fully interactive user interface. . Dynamic

    linking. . Built-in dynamic storage manage- ment. . Built-in facilities for manipulating lists and symbolic names. . Standard internal representation of programs as list data. . Powerful macro facility. . Extensive runtime environment. . Minimal syntax (as compared with ALGOL-like languages). . Typeless variables. LANGUAGE OVERVIEW Common Lisp Features . Fully lexically scoped variables. . A rich set of numberical data types. . A rich set of numerical primitives. . A string data type. . Arrays and vectors. . Bit and field manipulation. . Dynamic non-local exits, and user-controllable error handling. . Built-in hash facility. . User-defined data types. . Stream-based I/O with a relatively implementation- independent interface. . Formatting and pretty-printing utilities.
  5. 5 Example: a factorial function in C LQWIDFW LQWQ ^

    LQWVRIDU  ZKLOH Q! VRIDU Q UHWXUQVRIDU ` Common Lisp (version 1) GHIXQIDFW Q  OHW VRIDU  ORRSZKLOH !Q GR  VHWTVRIDU VRIDUQ  GHFIQ VRIDU Common Lisp (version 2) GHIXQIDFW [  LI  [   [ IDFW [ EXAMPLE: A FACTORIAL FUNCTION Common Lisp (version 3) GHIXQIDFW [  ODEHOV LWHU QDFF  LI  Q DFF  LWHU Q  QDFF  LWHU[ Common Lisp (version 4) GHIXQIDFW [  GHFODUH RSWLPL]HVSHHG  ODEHOV LWHU QDFF  GHFODUH W\SHଘ[QXPQDFF  LI  Q DFF  LWHU WKHଘ[QXP Q  WKHଘ[QXP QDFF  LWHU[
  6. 6 Multi-Paradigms . Imperative Programming (for, if, goto, ...) .

    Functional Programming (lambda) . Object-oriented programming (CLOS & MOP) . Generic programming (macro) . Aspect-oriented programming (AspectL) . Rule-based and Logic progamming (Knowledge- Works & Common Prolog) PROGRAMMING PARADIGMS GOTO in C LQWIDFW LQWQ ^ LQWVRIDU  D VRIDU Q LI Q! JRWRD UHWXUQVRIDU ` GOTO in Common Lisp GHIXQIDFW Q  OHW VRIDU  WDJERG\ D  VHWTVRIDU VRIDUQ  GHFIQ  LI !Q  JRD VRIDU
  7. 7 DATA TYPES IN COMMON LISP Data Types . Numbers

    . Characters . Symbols . Lists . Arrays . Hash tables . Readtables . Packages . Pathnames . Streams . Random-states . Structures . Functions . Conditions . Classes . Methods . Generic Functions number complex real rational float integer fixnum bignum signed-byte unsigned-byte bit short-float single-float double-float long-float ratio sequence list cons null (nil) vector string bit-vector data type = creation + operation array
  8. 8 PROGRAM STRUCTURE Forms and Functions . Forms: (+ 3

    4) . Self-Evaluating Forms: numbers, charac- ters, strings and bit-vectors. . Variables . Special Forms . Macros . Function calls . Named Functions: defun . Lambda-Expressions ODPEGD DE  DE   !
  9. 9 LIST PROCESSING ONE TWO THREE (ONE TWO THREE) (ONE

    . TWO) = (cons ‘one ‘two) Functions on Lists GHIXQJRRGUHYHUVH OVW  ODEHOV UHY OVWDFF  LI QXOOOVW DFF  UHY FGUOLVW  FRQV FDUOVW DFF  UHYOVWQLO ! VHWTOVW DEF $%& ! JRRGUHYHUVHOVW &%$ !OVW $%&
  10. 10 SEQUENCE OPERATIONS Map/Reduce: a sample use 7DUJHWD  D

     D  D Q  " GHIXQVTXDUHVXP VHT  ODEHOV VTXDUH [  [[  UHGXFH   PDS OLVW VTXDUHVHT ! VHWTD   ! VTXDUHVXPD  ! VHWTD    ! VTXDUHVXPD  4XHVWLRQD  D  D  D Q  " Another version GHIXQVTXDUHVXP VHT  UHGXFH   PDS OLVW ODPEGD [  [[ VHT
  11. 11 COMMON LISP OBJECT SYSTEM CLOS, PART I The CLOS

    Approaches . The Layered Approach . The Generic Function Approach . The Multiple Inheritance Approach . The Method Combination Approach . First-Class Objects 7KHÀUVWOHYHORIWKH2EMHFW6\VWHPSURYLGHVDSURJUDPPDWLF LQWHUIDFHWRREMHFWRULHQWHGSURJUDPPLQJ7KLVOHYHOLVGHVLJQHGWR PHHWWKHQHHGVRIPRVWVHUL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\QWD[
  12. 12 COMMON LISP OBJECT SYSTEM CLOS, PART II Multimethods VS.

    Method Overloading SXEOLFFODVV$^ SXEOLFYRLGIRR $D ^6\VWHPRXWSULQWOQ $$ ` SXEOLFYRLGIRR %E ^6\VWHPRXWSULQWOQ $% ` ` SXEOLFFODVV%H[WHQGV$^ SXEOLFYRLGIRR $D ^6\VWHPRXWSULQWOQ %$ ` SXEOLFYRLGIRR %E ^6\VWHPRXWSULQWOQ %% ` ` SXEOLFFODVV0DLQ^ SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJY ^ $REM DUJY>@HTXDOV $ "QHZ$ QHZ%  REMIRR REM  ` ` MDYDFRPJLJDPRQNH\V0DLQ$ $$ MDYDFRPJLJDPRQNH\V0DLQ% %$ CLOS Demo Code GHIFODVVD  GHIFODVVE D  GHIJHQHULFIRR DE GHIPHWKRGIRR DD  DD $$ GHIPHWKRGIRR DD  EE $% GHIPHWKRGIRR EE  DD %$ GHIPHWKRGIRR EE  EE %% ! GHIYDUD PDNHLQVWDQFH D ! GHIYDUE PDNHLQVWDQFH E ! IRRDD $$ ! IRREE %% ! IRRDE $% ! IRRED %$
  13. 13 COMMON LISP CONDITION SYSTEM Java-style exception handling WU\^ GR6WXII

     GR0RUH6WXII  `FDWFK 6RPH([FHSWLRQVH ^ UHFRYHU VH  ` or this in Python: WU\ GR6WXII GR0RUH6WXII H[FHSW6RPH([FHSWLRQVH UHFRYHU VH In Common Lisp you'd write this: KDQGOHUFDVH  SURJQ  GRVWXII  GRPRUHVWXII  VRPHH[FHSWLRQ VH  UHFRYHUVH Restart GHIXQIRR Q   UHVWDUWFDVH Q  XVHYDOXH [ [ ! IRR  ! IRR (UURU'LYLVLRQE\]HURFDXVHGE\RI    FRQWLQXH 5HWXUQDYDOXHWRXVH 6XSSO\QHZDUJXPHQWVWRXVH 86(9$/8(  DERUW 5HWXUQWROHYHO 5HWXUQWRWRSORRSOHYHO ! KDQGOHUELQG GLYLVLRQE\]HUR  ODPEGD LJQRUH  XVHYDOXH  IRR 
  14. 14 MAINTENANCE OF LISP IMAGE . Redefine variables: defvar v.s.

    defparameter . Redefine functions . Redefine macros . Delete unused objects . Change the classes of instances . Upgrade instaces for redefined class A example Suppose a class definition of triangle: GHIFODVVWULDQJOH VKDSH  DUHDGHUVLGHDLQLWDUJVLGHD  EUHDGHUVLGHELQLWDUJVLGHE  FUHDGHUVLGHFLQLWDUJVLGHF was changed to: GHIFODVVWULDQJOH VKDSH  DUHDGHUVLGHDLQLWDUJVLGHD  EUHDGHUVLGHELQLWDUJVLGHE  DQJOH&UHDGHUDQJOH&LQLWDUJDQJOH& GHIPHWKRGXSGDWHLQVWDQFHIRUUHGHଘQHGFODVVDIWHU  LQVWDQFHWULDQJOH DGGHGVORWVGLVFDUGHGVORWV SOLVW UHVWLQLWDUJV  GHFODUH LJQRUHLQLWDUJV  LI DQG PHPEHU FGLVFDUGHGVORWV  PHPEHU DQJOH&DGGHGVORWV  VHWI VORWYDOXHLQVWDQFH DQJOH&  WKUHHVLGHVWRDQJOH  JHWISOLVW F  VLGHDLQVWDQFH  VLGHELQVWDQFH GHIPHWKRGVLGHF WULWULDQJOH  WKLUGVLGH VLGHDWUL  VLGHEWUL  DQJOH&WUL $OJRULWKPLVFA DAEADE FRV& GHIXQWKLUGVLGH DEDQJOH&  VTUW   H[SWD  H[SWE  DE FRVDQJOH&
  15. 15 RUNTIME SYSTEM . Lisp application = System code +

    User code . Lisp image can be dumped into executions . Unused system code can be stripped out for saving application file size (optional) . Building process of Common Lisp systems: Bootstrap (mostly)
  16. 16 CL IMPLEMENTATIONS Common Lisp Implementations (not all!) NAME COMPILER

    SUPPORTING PLATFORMS ARCHITECTURE AVG. COST CMUCL Bytecode & Native Linux, Mac OS X, Solaris, FreeBSD, NetBSD Intel x86, SPARC Free SBCL Native Linux, Mac OS X, Solaris, FreeBSD, NetBSD, OpenBSD, Windows Intel x86, AMD64, PPC, SPARC, Alpha, MIPSbe, MIPEle Free CLISP Bytecode (old), Native Windows and all UNIX-like systems Most architectures Free ECL Native via C, also byte- code Linux, FreeBSD, NetBSD, OpenBSD, Solaris, Windows, Mac OS X, iOS Most architectures Free ABCL JVM Bytecode All platforms with JDK JDK supported architectures Free CLOZURE CL Native Mac OS X, Linux, FreeBSD, Solaris, Android, Windows Intel x86, AMD64, PPC, PPC64 Free MCL Native Mac OS 9, Mac OS X PPC Free since version 5.2 ALLEGRO CL Native Linux, Mac OS X, Windows, FreeBSD, Solaris Intel x86, AMD64, SPARC $599+ LISPWORKS Native Windows, Mac OS X, Linux, Solaris, AIX, HP-UX Intel x86, AMD64, PPC, PPC64, PA-RISC $1500 (pro), $5000 (ent) LIQUID CL Native Solaris, UP-UX, AIX SPARC, PA-RISC, IBM RS/6000 Unknown SCIENEER CL Native Linux, Solaris, HP-UX Intel x86, AMD64, SPARC, IA64, PA-RISC $299+ CORMAN LISP Native Windows Intel x86 $99 GCL Native via C Windows, Linux Most architectures Free XCL Native Windows, Linux, FreeBSD Intel x86, AMD64 Free
  17. 17 Books . Common Lisp: The Language, 2nd Edi- tion

    . Practical Common Lisp . On Lisp: Advanced Techniques for Com- mon LISP . Paradigms of Artificial Intelligence: Case study in Common Lisp . COMMON LISP: An Interactive Approach . Common LISP: a gentle introduction to symbolic computation . Object-Oriented Programming in Com- mon Lisp: A Programmer's Guide to CLOS . The Art of the Metaobject Protocol REFERENCES Papers . Recursive Functions of Symbolic Expressions and their Computation by Machine, Part I, by John McCarthy . History of LISP, by JohnMcCarthy . The Evolution of Lisp, by Guy L. Steele Jr. etc. . The Common Lisp Object System: An Over- view, by Linda G. DeMichiel . SBCL: a Sanely-Bootstrappable Common Lisp, by Christophe Rhodes Links . Common Lisp HyperSpec: http://www.lispworks.com/documentation/ HyperSpec/Front/index.htm