Lisp
                            Tijs van der Storm




Thursday, September 6, 12
About me...
                    • Work at Centrum Wiskunde & Informatica
                    • Teach at Universiteit van Amsterdam in the
                            Master Software Engineering
                    • According to @jvandenbos “typical esoteric
                            programming language dude” :)
                    • Contact: storm@cwi.nl, @tvdstorm

Thursday, September 6, 12
Interests and projects

                    • DSLs, MDD, programming, languages
                    • Co-designer of the Rascal
                            metaprogramming language
                    • Co-designer of the Ensō model-based
                            programming environment



Thursday, September 6, 12
Atze van der Ploeg




Thursday, September 6, 12
Today

                    • About Lisp
                    • Programming Clojure
                    • ... meta-programming



Thursday, September 6, 12
http://lambda.bugyo.tk/cdr/mwl/

Thursday, September 6, 12
What is Lisp?
                                                                    http://lisperati.com/
                    •       A programming language?
                    •       For LIst Processing?
                    •       The most intelligent way to misuse a computer?
                    •       Lots of Irritating Superfluous Parentheses?
                    •       Secret alien technology?
                    •       Oatmeal with fingernail clippings mixed in?
                    •       A programmer amplifier?


Thursday, September 6, 12
What is Lisp?

                    • A PL for building organisms (Perlis)
                    • Building material (Kay)
                    • Opposite of a Blub language (Graham)
                    • Maxwell’s equations of software (Kay)
                    • The greatest language ever invented (Kay)

Thursday, September 6, 12
John McCarthy




                                            (September 4, 1927 –
                                            October 24, 2011)

Thursday, September 6, 12
(




              Recursive Functions of Symbolic Expressions and
                   Their Computation by Machine, Part I
                              JOHX MCCAaTItY, Massachusetts Institute of Technology, Cambridge, Mass.


      1.   Introduction                                                2. F u n c t i o n s a n d F u n c t i o n Definitions
         A programming system called LISP (for lASt Processor)            We shMl need a number of mathematical ideas ar:d
      has been developed for the I B M 704 computer by the             notations concerning functions in general. Most of the
      Artificial Intelligence group at M.I.T. The system was           ideas are well known, but the notion of conditional e,~pre~'-
      designed to facilitate experiments with a proposed system        sion is believed to be new, and ihe use of conditional
      called the Advice Taker, whereby a machine could be              expressions permits functions to be defined recursively in a
      instructed to handle declarative as well as imperative           new and convenient way.
      sentences and could exhibit "common sense" in carrying              a. Partial Functions. A partial function is a funct on
      out its instructions. The original proposal It] for the Advice   that is defined only on part of its domain. Partial funetio:~s
      Taker was made in November 1958. The main require-               necessarily arise when functions are defined by eomputa~
      ment was a programming system for manipulating ex-               tions because for some values of the arguments t:he Pomp:>
      pressions representing formalized declarative and irnpera-       ration defining the value of the function may not ter-
      live sentences so that the Advice Taker system could make        minate. However, some of our elementary functions wilt be
      deductions.                                                      defined as partial functions.
         In the course of its development the Lisp system went
                                                                          b. Propositional Expres.s'ions and Predicates. A t)ropo~i-
      through several stages of simplification and eventually
                                                                       tionM expression is an expression whose possible values
      came to be based on a scheme for representing the partial
                                                                       are T (for truth) and F (for falsity). We shall assume
      recursive functions of a certain class of symbolic expres-
                                                                       that the reader is fanfiliar with the propositionM eom~ee-
      sions. This representation is independent of the IBM 704
                                                                       lives A ("and"), V ( " o r " ) , and ~ ( " n o t " ) , Typieai
      computer, or of any other electronic computer, and it now
                                                                       propositional expressions are:
      seems expedient to expound the system by starting with
      the class of expressions called S-expressions and the func-                              x<y
      tions called S-functions.
                                                                                              (x < y) A (b = e)
         In this article, we first describe a formalism for defining
      functions reeursively. We believe this formalism has ad-                                 x is prime
      vantages both as a programming language and as vehicle           A predicate is a function whose range consists of ih{:
      for developing a theory of computation. Next, we describe        truth values T and F.
      S-expressions and S-functions, give some examples, and
                                                                         e. Conditional Expressions. The dependence of truth
      then describe the universM S-function apply which plays
                                                                       values on the vahtes of quantities of other kinds is ex-
      the theoretical role of a universal Turing machine and
                                                                       pressed in mathematics by predicates, and the depende~ee
      the practical role of an interpreter. Then we describe the
                                                                       of truth values on other truth values by logical comxee-
      representation of S-expressions in the memmT of the
                                                                       ~ives. However, the notations for expressing symbol (alE"
      IBM 704 by list structures similar to those used by Newell,      the dependence of quantities of other kinds on trutt~
      Shaw and Simon [2], and the representation of S-functions        vMues is inadequate, so that English words and phrases
      by program. Then we mention the main features of the             are generMly used for expressing these depende~tces i:~
      Lisp programming system for the IBM 704. Next comes              texts that, describe other dependences symbolically. I!'<~r
      another way of describing computations with symbolic             example, the function Ix I is ustmlly defined in words.
      expressions, and finally we give a recursive function in-           Conditional expressions are a deviee for expressing the
      terpretation of flow charts.                                     dependence of quantities on propositional quantities. :
         We hope to describe some of the sylnbolie computations        conditional expression has the form
           Communications of the ACM, vol 3, issue 4, April 1960
      for which LISP has been used in another paper, and also to
      give elsewhere some applications of our reeursive function
                                                                                           (p: -+ el,    -.-   , p ~ --+ e , , )   http://dx.doi.org/10.1145/367177.367199
      formalism to mathematical logic and to the problem of            where the p's are propositionM expressions and the e's are
      mechanical theorem proving.                                      expressions of any kind. It may be read, "If p~ thexx <,
Thursday, September 6, 12
Last 17th of
                                                            August: 50 years
                                                                ago (!)




  http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
Thursday, September 6, 12
The famous
                             page 13




                             http://xkcd.com/917/

Thursday, September 6, 12
Guy L. Steele, Richard P. Gabriel, “The evolution of Lisp”, in: History of programming languages II, ACM 1996, p. 311


Thursday, September 6, 12
Revised5 Report on the Algorithmic Language
                                    Scheme
                                      RICHARD   KELSEY, WILLIAM CLINGER, AND JONATHAN REES (Editors)
                         H. ABELSON             R. K. DYBVIG             C. T. HAYNES           G. J. ROZAS
                         N. I. ADAMS IV         D. P. FRIEDMAN           E. KOHLBECKER          G. L. STEELE JR.
                         D. H. BARTLEY          R. HALSTEAD              D. OXLEY               G. J. SUSSMAN
                         G. BROOKS              C. HANSON                K. M. PITMAN           M. WAND


                                                     Dedicated to the Memory of Robert Hieb

                                20 February 1998                                                     CONTENTS
                                                                           Introduction . . . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .    2


                                                                                                                                                                        50 pages:
                                  SUMMARY                                  1 Overview of Scheme . . . . . . . . . . . . . . . .       .   .   .   .   .   .   .    3
                                                                               1.1 Semantics . . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .    3
             The report gives a defining description of the program-            1.2 Syntax . . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .    3
             ming language Scheme. Scheme is a statically scoped and           1.3 Notation and terminology . . . . . . . . .         .   .   .   .   .   .   .    3



                                                                                                                                                                       pure, small,
             properly tail-recursive dialect of the Lisp programming       2 Lexical conventions . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .    5
             language invented by Guy Lewis Steele Jr. and Gerald              2.1 Identifiers . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .    5
             Jay Sussman. It was designed to have an exceptionally             2.2 Whitespace and comments . . . . . . . . .          .   .   .   .   .   .   .    5
             clear and simple semantics and few di↵erent ways to form          2.3 Other notations . . . . . . . . . . . . . . .      .   .   .   .   .   .   .    5



                                                                                                                                                                       “academic”
                                                                           3 Basic concepts . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .    6
             expressions. A wide variety of programming paradigms, in-
                                                                               3.1 Variables, syntactic keywords, and regions         .   .   .   .   .   .   .    6
             cluding imperative, functional, and message passing styles,
                                                                               3.2 Disjointness of types . . . . . . . . . . . .      .   .   .   .   .   .   .    6
             find convenient expression in Scheme.                              3.3 External representations . . . . . . . . . .       .   .   .   .   .   .   .    6
             The introduction o↵ers a brief history of the language and        3.4 Storage model . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .    7
             of the report.                                                    3.5 Proper tail recursion . . . . . . . . . . . .      .   .   .   .   .   .   .    7
                                                                           4 Expressions . . . . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .    8
             The first three chapters present the fundamental ideas of          4.1 Primitive expression types . . . . . . . . .       .   .   .   .   .   .   .    8
             the language and describe the notational conventions used         4.2 Derived expression types . . . . . . . . . .       .   .   .   .   .   .   .   10
             for describing the language and for writing programs in the       4.3 Macros . . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   13
             language.                                                     5 Program structure . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   16
                                                                               5.1 Programs . . . . . . . . . . . . . . . . . .       .   .   .   .   .   .   .   16
             Chapters 4 and 5 describe the syntax and semantics of             5.2 Definitions . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   16
             expressions, programs, and definitions.                            5.3 Syntax definitions . . . . . . . . . . . . .        .   .   .   .   .   .   .   17
             Chapter 6 describes Scheme’s built-in procedures, which       6 Standard procedures . . . . . . . . . . . . . . .        .   .   .   .   .   .   .   17
             include all of the language’s data manipulation and in-           6.1 Equivalence predicates . . . . . . . . . . .       .   .   .   .   .   .   .   17
                                                                               6.2 Numbers . . . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   19
             put/output primitives.
                                                                               6.3 Other data types . . . . . . . . . . . . . .       .   .   .   .   .   .   .   25
             Chapter 7 provides a formal syntax for Scheme written in          6.4 Control features . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   31
             extended BNF, along with a formal denotational semantics.         6.5 Eval . . . . . . . . . . . . . . . . . . . . .     .   .   .   .   .   .   .   35
             An example of the use of the language follows the formal          6.6 Input and output . . . . . . . . . . . . . .       .   .   .   .   .   .   .   35
             syntax and semantics.                                         7 Formal syntax and semantics . . . . . . . . . . .        .   .   .   .   .   .   .   38
                                                                               7.1 Formal syntax . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   38
             The report concludes with a list of references and an al-         7.2 Formal semantics . . . . . . . . . . . . . .       .   .   .   .   .   .   .   40
             phabetic index.                                                   7.3 Derived expression types . . . . . . . . . .       .   .   .   .   .   .   .   43
                                                                           Notes . . . . . . . . . . . . . . . . . . . . . . . . .    .   .   .   .   .   .   .   45
                                                                           Additional material . . . . . . . . . . . . . . . . .      .   .   .   .   .   .   .   45
                                                                           Example . . . . . . . . . . . . . . . . . . . . . . . .    .   .   .   .   .   .   .   45
                                                                           References . . . . . . . . . . . . . . . . . . . . . . .   .   .   .   .   .   .   .   46
                                                                           Alphabetic index of definitions of concepts,
                                                                                   keywords, and procedures . . . . . . . . .         . . . . . . .               48




Thursday, September 6, 12
1029 pages:
                            comprehensive, practical,
                               messy, “industrial”




Thursday, September 6, 12
What made Lisp different?




                            http://paulgraham.com/diff.html

Thursday, September 6, 12
How is Lisp still different?
                    • Homoiconic syntax
                     • aka: there is no syntax
                    • Macros
                     • aka: compile-time code transformers
                    • Code is data, data can be code
                    • Program into the language
Thursday, September 6, 12
http://xkcd.com/224/


Thursday, September 6, 12
Thursday, September 6, 12
Fun resources




Thursday, September 6, 12
http://xkcd.com/297/


Thursday, September 6, 12
Rich Hickey




Thursday, September 6, 12
• Lisp syntax, macros, code as data etc.
                    • Functional programming, immutable data
                    • Data structures: map, set, vector, list
                    • Concurrency: transactional memory
                    • Compiles to JVM, intergrates with Java
                    • [and much more]
Thursday, September 6, 12
(def basic-data-types
             '{:booleans [true, false]
               :numbers [1, 2, 3.0, 4/5]
               :strings ["this is a string"]
               :symbols [a, empty?, +, user/foo]
               :keywords [:a-key-word]})


                                     NB: commas, are
                                      whitespace (!)
Thursday, September 6, 12
(def collection-types
                      '{:vectors [1,2,3,4]
                        :maps     {:x 3, :y 4}
                        :sets     #{a set of symbols}
                        :lists    (a list of symbols)})




Thursday, September 6, 12
• Expressed using lists (Polish notation):
                            (operator arg1 arg2 ...)

                    • Head is applied to the arguments in tail:
                            (+ 1 2)




Thursday, September 6, 12
Special forms
             define          (def x 3)


     conditional            (if (> x 1) 'then 'else)

                            (do
     sequencing               (print "hello")
                              (print "world!"))

      local vars            (let [x 1] (+ x 1))

                            (quote (this returns a list with seven symbols))
       quotation            '(this returns a list with seven symbols)

          closures          (fn [x n] (+ x n))




Thursday, September 6, 12
Convenience macros
                   define a       (defn power [x n]
                   function        (if (= n 0)
                                     1
                                     (* x (power x (- n 1)))))




                    define a       (defmacro unless [cond then else]
                     macro          `(if (not ~cond) ~then ~else))




Thursday, September 6, 12
Macros!
                    • Functions that transform code trees
                    • aka: code that writes code
                template       (defmacro unless [cond then else]
                                 `(if (not ~cond) ~then ~else))



                            quasi quote `         unquote ~



Thursday, September 6, 12
Trying it in the REPL
           => (defmacro unless [cond then else]
                `(if (not ~cond) ~then ~else))
           #'user/unless

           => (unless (> 2 3) 'yes 'no)
           yes

           => (macroexpand '(unless (> 2 3) 'yes 'no))
           (if (clojure.core/not (> 2 3)) (quote yes) (quote no))

           => (macroexpand '(unless (> 2 3) (+ 1 2) (* 2 3)))
           (if (clojure.core/not (> 2 3)) (+ 1 2) (* 2 3))




Thursday, September 6, 12
Cascading conditionals
    (cond' [(> x y) 1]
           [(< x y) -1]
           [(= x y) 0]))
                                                         rest params
                            (defmacro cond' [case & cases]
                              (if (empty? cases)
                                `(when ~(first case)           splicing
                                   ~(second case))           unquote ~@
                                `(if ~(first case)
                                   ~(second case)
              macro                (cond' ~(first cases) ~@(rest cases)))))
            recursion


Thursday, September 6, 12
Testing it out
                       => ((fn [x y] (cond'
                                       [(> x y) 1]
                                       [(< x y) -1]
                                       [(= x y) 0])) 1 2)
                       -1

                       => (macroexpand-all '(cond'
                                              [(> x   y) 1]
                                              [(< x   y) -1]
                                              [(= x   y) 0]))
                       (if (> x y) 1 (if (< x y) -1   (if (= x y) (do 0))))




Thursday, September 6, 12
Why is this cool?
                    • Extend the language with new abstractions
                     • control-flow
                     • state machine
                     • GUI builders
                     • grammars, ... etc.
                    • Reuse Lisp syntax / compile with macros
Thursday, September 6, 12
s




                            http://www.cwi.nl/~storm/devclj.html

Thursday, September 6, 12

Learn a language : LISP

  • 1.
    Lisp Tijs van der Storm Thursday, September 6, 12
  • 2.
    About me... • Work at Centrum Wiskunde & Informatica • Teach at Universiteit van Amsterdam in the Master Software Engineering • According to @jvandenbos “typical esoteric programming language dude” :) • Contact: storm@cwi.nl, @tvdstorm Thursday, September 6, 12
  • 3.
    Interests and projects • DSLs, MDD, programming, languages • Co-designer of the Rascal metaprogramming language • Co-designer of the Ensō model-based programming environment Thursday, September 6, 12
  • 4.
    Atze van derPloeg Thursday, September 6, 12
  • 5.
    Today • About Lisp • Programming Clojure • ... meta-programming Thursday, September 6, 12
  • 6.
  • 7.
    What is Lisp? http://lisperati.com/ • A programming language? • For LIst Processing? • The most intelligent way to misuse a computer? • Lots of Irritating Superfluous Parentheses? • Secret alien technology? • Oatmeal with fingernail clippings mixed in? • A programmer amplifier? Thursday, September 6, 12
  • 8.
    What is Lisp? • A PL for building organisms (Perlis) • Building material (Kay) • Opposite of a Blub language (Graham) • Maxwell’s equations of software (Kay) • The greatest language ever invented (Kay) Thursday, September 6, 12
  • 9.
    John McCarthy (September 4, 1927 – October 24, 2011) Thursday, September 6, 12
  • 10.
    ( Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I JOHX MCCAaTItY, Massachusetts Institute of Technology, Cambridge, Mass. 1. Introduction 2. F u n c t i o n s a n d F u n c t i o n Definitions A programming system called LISP (for lASt Processor) We shMl need a number of mathematical ideas ar:d has been developed for the I B M 704 computer by the notations concerning functions in general. Most of the Artificial Intelligence group at M.I.T. The system was ideas are well known, but the notion of conditional e,~pre~'- designed to facilitate experiments with a proposed system sion is believed to be new, and ihe use of conditional called the Advice Taker, whereby a machine could be expressions permits functions to be defined recursively in a instructed to handle declarative as well as imperative new and convenient way. sentences and could exhibit "common sense" in carrying a. Partial Functions. A partial function is a funct on out its instructions. The original proposal It] for the Advice that is defined only on part of its domain. Partial funetio:~s Taker was made in November 1958. The main require- necessarily arise when functions are defined by eomputa~ ment was a programming system for manipulating ex- tions because for some values of the arguments t:he Pomp:> pressions representing formalized declarative and irnpera- ration defining the value of the function may not ter- live sentences so that the Advice Taker system could make minate. However, some of our elementary functions wilt be deductions. defined as partial functions. In the course of its development the Lisp system went b. Propositional Expres.s'ions and Predicates. A t)ropo~i- through several stages of simplification and eventually tionM expression is an expression whose possible values came to be based on a scheme for representing the partial are T (for truth) and F (for falsity). We shall assume recursive functions of a certain class of symbolic expres- that the reader is fanfiliar with the propositionM eom~ee- sions. This representation is independent of the IBM 704 lives A ("and"), V ( " o r " ) , and ~ ( " n o t " ) , Typieai computer, or of any other electronic computer, and it now propositional expressions are: seems expedient to expound the system by starting with the class of expressions called S-expressions and the func- x<y tions called S-functions. (x < y) A (b = e) In this article, we first describe a formalism for defining functions reeursively. We believe this formalism has ad- x is prime vantages both as a programming language and as vehicle A predicate is a function whose range consists of ih{: for developing a theory of computation. Next, we describe truth values T and F. S-expressions and S-functions, give some examples, and e. Conditional Expressions. The dependence of truth then describe the universM S-function apply which plays values on the vahtes of quantities of other kinds is ex- the theoretical role of a universal Turing machine and pressed in mathematics by predicates, and the depende~ee the practical role of an interpreter. Then we describe the of truth values on other truth values by logical comxee- representation of S-expressions in the memmT of the ~ives. However, the notations for expressing symbol (alE" IBM 704 by list structures similar to those used by Newell, the dependence of quantities of other kinds on trutt~ Shaw and Simon [2], and the representation of S-functions vMues is inadequate, so that English words and phrases by program. Then we mention the main features of the are generMly used for expressing these depende~tces i:~ Lisp programming system for the IBM 704. Next comes texts that, describe other dependences symbolically. I!'<~r another way of describing computations with symbolic example, the function Ix I is ustmlly defined in words. expressions, and finally we give a recursive function in- Conditional expressions are a deviee for expressing the terpretation of flow charts. dependence of quantities on propositional quantities. : We hope to describe some of the sylnbolie computations conditional expression has the form Communications of the ACM, vol 3, issue 4, April 1960 for which LISP has been used in another paper, and also to give elsewhere some applications of our reeursive function (p: -+ el, -.- , p ~ --+ e , , ) http://dx.doi.org/10.1145/367177.367199 formalism to mathematical logic and to the problem of where the p's are propositionM expressions and the e's are mechanical theorem proving. expressions of any kind. It may be read, "If p~ thexx <, Thursday, September 6, 12
  • 11.
    Last 17th of August: 50 years ago (!) http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf Thursday, September 6, 12
  • 12.
    The famous page 13 http://xkcd.com/917/ Thursday, September 6, 12
  • 13.
    Guy L. Steele,Richard P. Gabriel, “The evolution of Lisp”, in: History of programming languages II, ACM 1996, p. 311 Thursday, September 6, 12
  • 14.
    Revised5 Report onthe Algorithmic Language Scheme RICHARD KELSEY, WILLIAM CLINGER, AND JONATHAN REES (Editors) H. ABELSON R. K. DYBVIG C. T. HAYNES G. J. ROZAS N. I. ADAMS IV D. P. FRIEDMAN E. KOHLBECKER G. L. STEELE JR. D. H. BARTLEY R. HALSTEAD D. OXLEY G. J. SUSSMAN G. BROOKS C. HANSON K. M. PITMAN M. WAND Dedicated to the Memory of Robert Hieb 20 February 1998 CONTENTS Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 50 pages: SUMMARY 1 Overview of Scheme . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 Semantics . . . . . . . . . . . . . . . . . . . . . . . . . 3 The report gives a defining description of the program- 1.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ming language Scheme. Scheme is a statically scoped and 1.3 Notation and terminology . . . . . . . . . . . . . . . . 3 pure, small, properly tail-recursive dialect of the Lisp programming 2 Lexical conventions . . . . . . . . . . . . . . . . . . . . . . . 5 language invented by Guy Lewis Steele Jr. and Gerald 2.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . 5 Jay Sussman. It was designed to have an exceptionally 2.2 Whitespace and comments . . . . . . . . . . . . . . . . 5 clear and simple semantics and few di↵erent ways to form 2.3 Other notations . . . . . . . . . . . . . . . . . . . . . . 5 “academic” 3 Basic concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 6 expressions. A wide variety of programming paradigms, in- 3.1 Variables, syntactic keywords, and regions . . . . . . . 6 cluding imperative, functional, and message passing styles, 3.2 Disjointness of types . . . . . . . . . . . . . . . . . . . 6 find convenient expression in Scheme. 3.3 External representations . . . . . . . . . . . . . . . . . 6 The introduction o↵ers a brief history of the language and 3.4 Storage model . . . . . . . . . . . . . . . . . . . . . . . 7 of the report. 3.5 Proper tail recursion . . . . . . . . . . . . . . . . . . . 7 4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The first three chapters present the fundamental ideas of 4.1 Primitive expression types . . . . . . . . . . . . . . . . 8 the language and describe the notational conventions used 4.2 Derived expression types . . . . . . . . . . . . . . . . . 10 for describing the language and for writing programs in the 4.3 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 language. 5 Program structure . . . . . . . . . . . . . . . . . . . . . . . . 16 5.1 Programs . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapters 4 and 5 describe the syntax and semantics of 5.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 16 expressions, programs, and definitions. 5.3 Syntax definitions . . . . . . . . . . . . . . . . . . . . 17 Chapter 6 describes Scheme’s built-in procedures, which 6 Standard procedures . . . . . . . . . . . . . . . . . . . . . . 17 include all of the language’s data manipulation and in- 6.1 Equivalence predicates . . . . . . . . . . . . . . . . . . 17 6.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 19 put/output primitives. 6.3 Other data types . . . . . . . . . . . . . . . . . . . . . 25 Chapter 7 provides a formal syntax for Scheme written in 6.4 Control features . . . . . . . . . . . . . . . . . . . . . . 31 extended BNF, along with a formal denotational semantics. 6.5 Eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 An example of the use of the language follows the formal 6.6 Input and output . . . . . . . . . . . . . . . . . . . . . 35 syntax and semantics. 7 Formal syntax and semantics . . . . . . . . . . . . . . . . . . 38 7.1 Formal syntax . . . . . . . . . . . . . . . . . . . . . . . 38 The report concludes with a list of references and an al- 7.2 Formal semantics . . . . . . . . . . . . . . . . . . . . . 40 phabetic index. 7.3 Derived expression types . . . . . . . . . . . . . . . . . 43 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Additional material . . . . . . . . . . . . . . . . . . . . . . . . 45 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Alphabetic index of definitions of concepts, keywords, and procedures . . . . . . . . . . . . . . . . 48 Thursday, September 6, 12
  • 15.
    1029 pages: comprehensive, practical, messy, “industrial” Thursday, September 6, 12
  • 16.
    What made Lispdifferent? http://paulgraham.com/diff.html Thursday, September 6, 12
  • 17.
    How is Lispstill different? • Homoiconic syntax • aka: there is no syntax • Macros • aka: compile-time code transformers • Code is data, data can be code • Program into the language Thursday, September 6, 12
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    • Lisp syntax,macros, code as data etc. • Functional programming, immutable data • Data structures: map, set, vector, list • Concurrency: transactional memory • Compiles to JVM, intergrates with Java • [and much more] Thursday, September 6, 12
  • 24.
    (def basic-data-types '{:booleans [true, false] :numbers [1, 2, 3.0, 4/5] :strings ["this is a string"] :symbols [a, empty?, +, user/foo] :keywords [:a-key-word]}) NB: commas, are whitespace (!) Thursday, September 6, 12
  • 25.
    (def collection-types '{:vectors [1,2,3,4] :maps {:x 3, :y 4} :sets #{a set of symbols} :lists (a list of symbols)}) Thursday, September 6, 12
  • 26.
    • Expressed usinglists (Polish notation): (operator arg1 arg2 ...) • Head is applied to the arguments in tail: (+ 1 2) Thursday, September 6, 12
  • 27.
    Special forms define (def x 3) conditional (if (> x 1) 'then 'else) (do sequencing (print "hello") (print "world!")) local vars (let [x 1] (+ x 1)) (quote (this returns a list with seven symbols)) quotation '(this returns a list with seven symbols) closures (fn [x n] (+ x n)) Thursday, September 6, 12
  • 28.
    Convenience macros define a (defn power [x n] function (if (= n 0) 1 (* x (power x (- n 1))))) define a (defmacro unless [cond then else] macro `(if (not ~cond) ~then ~else)) Thursday, September 6, 12
  • 29.
    Macros! • Functions that transform code trees • aka: code that writes code template (defmacro unless [cond then else] `(if (not ~cond) ~then ~else)) quasi quote ` unquote ~ Thursday, September 6, 12
  • 30.
    Trying it inthe REPL => (defmacro unless [cond then else] `(if (not ~cond) ~then ~else)) #'user/unless => (unless (> 2 3) 'yes 'no) yes => (macroexpand '(unless (> 2 3) 'yes 'no)) (if (clojure.core/not (> 2 3)) (quote yes) (quote no)) => (macroexpand '(unless (> 2 3) (+ 1 2) (* 2 3))) (if (clojure.core/not (> 2 3)) (+ 1 2) (* 2 3)) Thursday, September 6, 12
  • 31.
    Cascading conditionals (cond' [(> x y) 1] [(< x y) -1] [(= x y) 0])) rest params (defmacro cond' [case & cases] (if (empty? cases) `(when ~(first case) splicing ~(second case)) unquote ~@ `(if ~(first case) ~(second case) macro (cond' ~(first cases) ~@(rest cases))))) recursion Thursday, September 6, 12
  • 32.
    Testing it out => ((fn [x y] (cond' [(> x y) 1] [(< x y) -1] [(= x y) 0])) 1 2) -1 => (macroexpand-all '(cond' [(> x y) 1] [(< x y) -1] [(= x y) 0])) (if (> x y) 1 (if (< x y) -1 (if (= x y) (do 0)))) Thursday, September 6, 12
  • 33.
    Why is thiscool? • Extend the language with new abstractions • control-flow • state machine • GUI builders • grammars, ... etc. • Reuse Lisp syntax / compile with macros Thursday, September 6, 12
  • 34.
    s http://www.cwi.nl/~storm/devclj.html Thursday, September 6, 12