Save 37% off PRO during our Black Friday Sale! »

The Nature of Lisp

955a5fe02c974f1ccb7ed1c4590c1d64?s=47 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/

955a5fe02c974f1ccb7ed1c4590c1d64?s=128

bloc

November 12, 2015
Tweet

Transcript

  1. The Nature of Lisp Adapted from http://www.defmacro.org/ramblings/ lisp.html

  2. “Unfortunately, nobody can be told what the matrix is. You

    have to see it for yourself.”
  3. 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>
  4. XML = Tree structure • need a graphic

  5. What else is a tree?

  6. Source code is a tree int add(int arg1, int arg2)

    { return arg1 + arg2; }
  7. This is universal • We can do this with any

    language! • Source code -> XML -> other source code • Ruby -> XML -> Javascript?! • Ever heard of emscripten?
  8. So what?

  9. <define-function return-type="int" name="add"> <arguments> <argument type="int">arg1</argument> <argument type="int">arg2</argument> </arguments> <body>

    <return> <add value1="arg1" value2="arg2" /> </return> </body> </define>
  10. Code = Data • If code is data, does that

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

  12. Example <copy todir="../new/dir"> <fileset dir="src_dir"/> </copy>

  13. Why XML?

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

    Ruby, or pick a language) • How would we represent that “copy” task in Java?
  15. 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>
  16. copy("../new/dir") { fileset("src_dir"); }

  17. 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
  18. Interesting Question • Can we extend Ant in Ant? •

    Consider a general purpose “Task” Element type: <task name="Test"> <echo message="Hello World!"/> </task> <Test />
  19. Improving upon XML

  20. S-expressions • Another format for arbitrary hierarchical data • Less

    verbose than XML • Much better suited to typing source code
  21. Copy revisited <copy todir="../new/dir"> <fileset dir="src_dir"/> </copy>

  22. Copy revisited <copy> <todir>../new/dir</todir> <fileset> <dir>src_dir</dir> </fileset> </copy>

  23. Back to s-expressions (copy (todir "../new/dir") (fileset (dir "src_dir")))

  24. Task in XML <task name="Test"> <echo message="Hello World!"/> </task> <Test

    />
  25. Task in s-expressions (task (name "Test") (echo (message "Hello World!")))

    (Test)
  26. Break it down (task, (name, "test"), (echo, (message, "Hello World!")))

  27. Hello, Lisp • Prefix Notation • Symbols • Lists •

    Functions • Macros
  28. 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))
  29. 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
  30. Symbols (Examples) • (set test 5) • (set = 5)

    • (set test “hello”) • (set * “hello”)
  31. 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.
  32. Functions • (fn (a) (* a 2))

  33. Functions • (fn (a) (* a 2)) • (set times-two

    (fn (a) (* a 2)))
  34. Functions • (fn (a) (* a 2)) • (set times-two

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

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

    “hello”) • (test (1 2) “hello”)
  37. 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.
  38. 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))
  39. Lists as data • (set test ‘(1 2)) ; correct

    • (set test (1 2)) ; error • (set test ‘(* 3 4)) ; correct
  40. 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) ; ?????
  41. Lisp Macros

  42. 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>
  43. Remember our todo list? (todo "housework" (item (priority high) "Clean

    the house.") (item (priority medium) "Wash the dishes.") (item (priority medium) "Buy more soap."))
  44. Reminder: functions (function-name arg1 arg2 arg3)

  45. A macro is the same as a function, except its

    arguments are not evaluated.
  46. (macro-name (+ 4 5))

  47. 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
  48. An Example Macro (defmacro triple (x) '(+ ~x ~x ~x))

  49. Every time (triple 4) is encountered in the code, it

    will be replaced with (+ 4 4 4)
  50. 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."))
  51. item macro (defmacro item (priority note) '(block (print stdout tab

    "Priority: " ~(head (tail priority)) endl) (print stdout tab "Note: " ~note endl endl)))
  52. 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?
  53. None