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)
Transcript of "Discovering joy"
on Discovering Joy Francis Fish (@fjfish)
Fish?NOT COMPUTER SCIENTISTLIKE PROGRAMMINGLEARN NEW LANGUAGE LONG TIMENOWKNOW NOTHING, YOU MUCH CLEVERERTHAN MEDISCOVERING JOY - NOT EXPERT!
Blame BraithwaiteAppendix Finding joy in combinators
Combinator?http://en.wikipedia.org/wiki/CombinatorA combinator is a higher-order function that usesonly function application and earlier definedcombinators to define a result from its arguments.(no parameters, functions as arguments)
Joy Resourceshttp://en.wikipedia.org/wiki/Joy_(programminInvented by Manfred von Thun of La TrobeUniversity in Melbourne, Australia (retired)Purely functional, combinatorial, stack basedMirrorhttp://www.kevinalbrecht.com/code/joy-mirror
InterestingProgram is data (like Lisp)Stack based - no parametersPost-fix (goes with stack)Does functional stuff wellCombinators - take what’s on the stack and dostuff - including combining other combinatorsNot Forth - no dictionary - functional
ProgramsJoy programs are built from smaller programs byjust two operations: concatenation and quotation.
Quoted Programs[bob alice]Is a list, but also a quoted programYou can plug it into things and then executeBuilt in functions, anonymous Y combinatoretc.Fill in the blanks
Typescharacter, file, float, integer, list, set, string,truthWhat else do you need?(Except maybe hash)And sets are weird
Combinatorsifte - if then elseThe 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 topdipdip 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.iidentity - run the quoted program on the top of the stack
CombinatorsconcatJoin two aggregates togethercons - lispAdd the top thing to the aggregate behind it on the stackdupDuplicate the stack topswapcause a flight of penguins
Primitive Recursion5  [*] 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)
Y-Combinator5[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat dup cons i
Explanation5 Cond Else Then[ [pop 0 =] [pop pop 1] [[dup 1 -] dip i *] ifte ][dup cons] swap concat dup cons i
If the top of the stack is 0 discard the copied program and the 0 left andpush 1else duplicate the top of the stack and subtract 1 fromthe top valuethen dip and call the copy of yourself underneaththe stack topon return apply * * the value below with the one you calculated
[dup cons] swap concat program and the integer on(We already have the quoted dup cons ithe 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 underneathdup the aggregateconcat them together swap with whatever’s on the stacktopWe end up with the previous quoted program on the stacktop and run it
Ruby PracticeHow do we do recursion? We can see the stack, this reminds us of things we had forgottenRecursion can always be recast as iteration Quick poll - how many people know this? How many had forgotten?Can make difficult problems more tractable
Ruby PracticeProgram is data? Method missing/symbols Dispatch tables etc. ERB - macro languages
THings to think aboutCan we use stacks and combinators to solveproblems more easily?Can we create DSL’s that do this?Would we want to :)
Building a web app Who needs HAML when we could have:[ [ [ ["Hello" div] ] body ] [ [ [“My App” title] [“main.js” script] ] head] ] html
Next?http://programjoy.eu - £1.40 :)Will front a runtime and links to the mirrorWill run the tutorialIf you want to help - contact meMore fun to work on rather than <corporate-bs>next big ecommerce social doo dahsite</corporate-bs>
Finis/Questions Francis Fish (@fjfish) www.francisfish.com www.leanpub.com/fjfish
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.