Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

“Unfortunately, nobody can be told what the matrix is. You have to see it for yourself.”

Slide 3

Slide 3 text

XML Reloaded Clean the house. Wash the dishes. Buy more soap.

Slide 4

Slide 4 text

XML = Tree structure • need a graphic

Slide 5

Slide 5 text

What else is a tree?

Slide 6

Slide 6 text

Source code is a tree int add(int arg1, int arg2) { return arg1 + arg2; }

Slide 7

Slide 7 text

This is universal • We can do this with any language! • Source code -> XML -> other source code • Ruby -> XML -> Javascript?! • Ever heard of emscripten?

Slide 8

Slide 8 text

So what?

Slide 9

Slide 9 text

arg1 arg2

Slide 10

Slide 10 text

Code = Data • If code is data, does that also mean data is code? • Head ‘splode

Slide 11

Slide 11 text

Ant

Slide 12

Slide 12 text

Example

Slide 13

Slide 13 text

Why XML?

Slide 14

Slide 14 text

Why XML? • FAR more flexible than writing java (or Ruby, or pick a language) • How would we represent that “copy” task in Java?

Slide 15

Slide 15 text

XML -> Java CopyTask copy = new CopyTask(); Fileset fileset = new Fileset(); fileset.setDir("src_dir"); copy.setToDir("../new/dir"); copy.setFileset(fileset); copy.execute();

Slide 16

Slide 16 text

copy("../new/dir") { fileset("src_dir"); }

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Interesting Question • Can we extend Ant in Ant? • Consider a general purpose “Task” Element type:

Slide 19

Slide 19 text

Improving upon XML

Slide 20

Slide 20 text

S-expressions • Another format for arbitrary hierarchical data • Less verbose than XML • Much better suited to typing source code

Slide 21

Slide 21 text

Copy revisited

Slide 22

Slide 22 text

Copy revisited ../new/dir src_dir

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Task in XML

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Hello, Lisp • Prefix Notation • Symbols • Lists • Functions • Macros

Slide 28

Slide 28 text

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))

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Symbols (Examples) • (set test 5) • (set = 5) • (set test “hello”) • (set * “hello”)

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

Functions • (fn (a) (* a 2))

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Functions • (fn (a) (* a 2)) • (set times-two (fn (a) (* a 2))) • (times-two 2) ; evaluates to 4

Slide 35

Slide 35 text

Lists • Pieces of XML written in s-expression form • Surrounded by parenthesis, separated by spaces, contains lisp data types

Slide 36

Slide 36 text

Lists • () • (1) • (1 “test”) • (test “hello”) • (test (1 2) “hello”)

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

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))

Slide 39

Slide 39 text

Lists as data • (set test ‘(1 2)) ; correct • (set test (1 2)) ; error • (set test ‘(* 3 4)) ; correct

Slide 40

Slide 40 text

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) ; ?????

Slide 41

Slide 41 text

Lisp Macros

Slide 42

Slide 42 text

Remember our todo list? Clean the house. Wash the dishes. Buy more soap.

Slide 43

Slide 43 text

Remember our todo list? (todo "housework" (item (priority high) "Clean the house.") (item (priority medium) "Wash the dishes.") (item (priority medium) "Buy more soap."))

Slide 44

Slide 44 text

Reminder: functions (function-name arg1 arg2 arg3)

Slide 45

Slide 45 text

A macro is the same as a function, except its arguments are not evaluated.

Slide 46

Slide 46 text

(macro-name (+ 4 5))

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

An Example Macro (defmacro triple (x) '(+ ~x ~x ~x))

Slide 49

Slide 49 text

Every time (triple 4) is encountered in the code, it will be replaced with (+ 4 4 4)

Slide 50

Slide 50 text

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."))

Slide 51

Slide 51 text

item macro (defmacro item (priority note) '(block (print stdout tab "Priority: " ~(head (tail priority)) endl) (print stdout tab "Note: " ~note endl endl)))

Slide 52

Slide 52 text

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?

Slide 53

Slide 53 text

No content