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

The Nature of Lisp

bloc
November 12, 2015

The Nature of Lisp

Dave Paola talks about the Lisp programming language, in a talk adapted from Slava Akhmechet's blog post: http://www.defmacro.org/ramblings/lisp.html

Full Presentation: http://code.bloc.io/the-nature-of-lisp/

bloc

November 12, 2015
Tweet

More Decks by bloc

Other Decks in Programming

Transcript

  1. XML Reloaded <todo name="housework"> <item priority="high">Clean the house.</item> <item priority="medium">Wash

    the dishes.</item> <item priority="medium">Buy more soap.</item> </todo>
  2. This is universal • We can do this with any

    language! • Source code -> XML -> other source code • Ruby -> XML -> Javascript?! • Ever heard of emscripten?
  3. Code = Data • If code is data, does that

    also mean data is code? • Head ‘splode
  4. Ant

  5. Why XML? • FAR more flexible than writing java (or

    Ruby, or pick a language) • How would we represent that “copy” task in Java?
  6. XML -> Java CopyTask copy = new CopyTask(); Fileset fileset

    = new Fileset(); fileset.setDir("src_dir"); copy.setToDir("../new/dir"); copy.setFileset(fileset); copy.execute(); <copy todir="../new/dir"> <fileset dir="src_dir"/> </copy>
  7. Extending Ant • Add a Java class, and then your

    XML can include that Element type • Ant really just matches Element tag names in XML with Java classes, and executes what it finds
  8. Interesting Question • Can we extend Ant in Ant? •

    Consider a general purpose “Task” Element type: <task name="Test"> <echo message="Hello World!"/> </task> <Test />
  9. S-expressions • Another format for arbitrary hierarchical data • Less

    verbose than XML • Much better suited to typing source code
  10. Prefix Notation • In Java, Ruby and every other languages,

    functions are invoked like this: • add(1,2) • add(1,multiply(2,3)) • In Lisp, functions are invoked using prefix notation, like this: • (add 1 (multiply 2 3))
  11. Symbols • Name used to access a variable • Like

    identifiers in other languages, except more liberal: not limited to alphanumeric + underscore • Examples: • + • = • hello-world • * • currentCity • You can assign symbols to any data type
  12. Symbols (Examples) • (set test 5) • (set = 5)

    • (set test “hello”) • (set * “hello”)
  13. Functions • Just like any other data type, like an

    integer, string, or symbol • Doesn’t have the notion of a name, like in other languages • You give a function a name by assigning it to a symbol, just like an integer or string • There is a special function for building functions. It is appropriately called fn.
  14. Functions • (fn (a) (* a 2)) • (set times-two

    (fn (a) (* a 2))) • (times-two 2) ; evaluates to 4
  15. Lists • Pieces of XML written in s-expression form •

    Surrounded by parenthesis, separated by spaces, contains lisp data types
  16. Lists • () • (1) • (1 “test”) • (test

    “hello”) • (test (1 2) “hello”)
  17. How Lisp Works • First element of the list is

    treated as a function • the rest of the elements are treated as parameters • If one of the elements is another list, it is executed in exactly the same manner and the result is passed to the original function • That’s it.
  18. Examples • (* 3 4) • (times-two 5) • (times-two)

    ; error, missing param • (3 4) ; error: 3 is not a function • (times-two 3 4) • (set + -) • (+ 5 4) • (* 3 (* 2 2))
  19. Lists as data • (set test ‘(1 2)) ; correct

    • (set test (1 2)) ; error • (set test ‘(* 3 4)) ; correct
  20. More Lisp • head returns the first element in a

    list • (head ‘(* 3 4)) ; returns * symbol • tail returns the rest of the list • (tail ‘(* 3 4)) ; returns (3 4) • (head (tail ‘(* 3 4))) ; ???? • (set test ‘(1 2 3)) • (head test) ; ?????
  21. Remember our todo list? <todo name="housework"> <item priority="high">Clean the house.</item>

    <item priority="medium">Wash the dishes.</item> <item priority="medium">Buy more soap.</item> </todo>
  22. Remember our todo list? (todo "housework" (item (priority high) "Clean

    the house.") (item (priority medium) "Wash the dishes.") (item (priority medium) "Buy more soap."))
  23. A macro is the same as a function, except its

    arguments are not evaluated.
  24. Uh, what? • Macros are little bits of code that

    generate code (using list abstraction) • We could create macros to-do and item that replace our data with whatever code we like • for instance, code that displays the item to the user • The benefit: we don’t have to walk the list! • The compiler will do it for us, invoking the appropriate macros. • All we need to do is create the macros that convert the data to appropriate code
  25. Back to our todo list (todo "housework" (item (priority high)

    "Clean the house.") (item (priority medium) "Wash the dishes.") (item (priority medium) "Buy more soap."))
  26. item macro (defmacro item (priority note) '(block (print stdout tab

    "Priority: " ~(head (tail priority)) endl) (print stdout tab "Note: " ~note endl endl)))
  27. Food for thought • What would an HTML page look

    like as s- expressions? • What would it mean to “execute” an s- expression encoded web page?