2. Fish?
NOT COMPUTER SCIENTIST
LIKE PROGRAMMING
LEARN NEW LANGUAGE LONG TIME
NOW
KNOW NOTHING, YOU MUCH CLEVERER
THAN ME
DISCOVERING JOY - NOT EXPERT!
7. Interesting
Program is data (like Lisp)
Stack based - no parameters
Post-fix (goes with stack)
Does functional stuff well
Combinators - take what’s on the stack and do
stuff - including combining other combinators
Not Forth - no dictionary - functional
14. Programs
Joy programs are built from smaller programs by
just two operations: concatenation and quotation.
15. Quoted Programs
[bob alice]
Is a list, but also a quoted program
You can plug it into things and then execute
Built in functions, anonymous Y combinator
etc.
Fill in the blanks
16. Types
character, file, float, integer, list, set, string,
truth
What else do you need?
(Except maybe hash)
And sets are weird
17. Combinators
ifte - if then else
The ifte combinator expects three quoted programs on the stack, an if-part, a then-part and an else-part,
in that order, with the else-part on top
dip
dip combinator expects a program on top of the stack and below that another value. It saves the value,
executes the program on the remainder of the stack and then restores the saved value.
i
identity - run the quoted program on the top of the stack
18. Combinators
concat
Join two aggregates together
cons - lisp
Add the top thing to the aggregate behind it on the stack
dup
Duplicate the stack top
swap
cause a flight of penguins
22. Primitive Recursion
5 [1] [*] primrec .
["R0" 1 1 2 3 4 5]
["*" 1 2 3 4 5] primrec : X [I] [C] -> R.Executes I to obtain
["*" 2 3 4 5] an initial value R0.For integer X uses increasing
positive integers to X, combines by C for new
["*" 6 4 5] R.For aggregate X uses successive members and
["*" 24 5] combines by C for new R.
["*" 120]
120
(HINT: it’s not recursion)
23. Y-Combinator
5
[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ]
[dup cons] swap concat dup cons i
24. Explanation
5 Cond Else Then
[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ]
[dup cons] swap concat dup cons i
25. If the top of the stack is 0
discard the copied program and the 0 left and
push 1
else
duplicate the top of the stack and subtract 1 from
the top value
then dip and call the copy of yourself underneath
the stack top
on return apply *
* the value below with the one you calculated
26. [dup cons] swap concat program and the integer on
(We already have the quoted dup cons i
the top of the stack)
(We push a quoted dup cons onto the stack)
run whats comes out of:
Take the stack top and cons to aggregate underneath
dup the aggregate
concat them together swap with whatever’s on the stack
top
We end up with the previous quoted program on the stack
top and run it
29. Ruby Practice
How do we do recursion?
We can see the stack, this reminds us of
things we had forgotten
Recursion can always be recast as iteration
Quick poll - how many people know this?
How many had forgotten?
Can make difficult problems more tractable
30. Ruby Practice
Program is data?
Method missing/symbols
Dispatch tables etc.
ERB - macro languages
31. THings to think about
Can we use stacks and combinators to solve
problems more easily?
Can we create DSL’s that do this?
Would we want to :)
32. Building a web app
Who needs HAML when we could have:
[
[
[
["Hello" div]
] body
]
[
[
[“My App” title]
[“main.js” script]
] head]
] html
33. Next?
http://programjoy.eu - £1.40 :)
Will front a runtime and links to the mirror
Will run the tutorial
If you want to help - contact me
More fun to work on rather than <corporate-
bs>next big ecommerce social doo dah
site</corporate-bs>
34. Finis/Questions
Francis Fish (@fjfish)
www.francisfish.com
www.leanpub.com/fjfish
Notes de l'éditeur
Brief aside - combinator names come from the puzzle book. First order logic is the Mathematics professor version. Birds are a tribute to Haskell Curry’s being a twitcher.
Forth small processors, lots of history, build language up from primitives and down from domain and meet in the middle - just like with Ruby DSL’s
5 dup dup * * .
[P] [T] [R1] [R2] If P is true T else Call R1 with self R2
If the data parameter is zero, then the first quotation has to produce the value to be returned. If the data parameter is positive then the second has to combine the data parameter with the result of applying the function to its predecessor.
UNPICK ON NEXT SLIDE - don’t jump the gun
HAML marvy - JS Frameworks etc. Quote lisp easier (maybe)