SlideShare une entreprise Scribd logo
1  sur  73
Télécharger pour lire hors ligne
We are OUDL.
            Organization for the Understanding of Dynamic Languages
                            http://meetup.com/dynamic/




Wednesday, August 22, 12

We are programming language enthusiasts
Check us out on Meetup.com
All events have been by members, for members
Each event has a theme, a selected pastry or baked good, and a horrible logo
What makes Objective C dynamic?




Wednesday, August 22, 12
What makes Objective C dynamic?        Kamehameha Bakery donuts



                                  Otto Cake cheesecake




                           Cake Couture cupcakes




                             Fendu Bakery croissants & cookies




                                             Saint Germain Bakery palmiers



Wednesday, August 22, 12
Mahalo.




Wednesday, August 22, 12
Y combinator
                                  Examples in Clojure.
                           Also includes: blenders and kittens.
                  Caveat emptor: I make no effort to teach you Clojure.


                                                                   Kyle Oba
                                                               @mudphone
                                                            Pas de Chocolat
Wednesday, August 22, 12
Not this one.
Wednesday, August 22, 12

Paul Graham did name his company after the *REAL* Y combinator.
But, why?
This one.
                 (defn Y
                   [g]
                   ((fn [x] (x x)) (fn [x]
                                    (g (fn [y] ((x x) y))))))




Wednesday, August 22, 12

Which is this thing, in Clojure.
Let’s get started.

                   Here’s a non-recursive
                   definition of factorial,
                          using the
                      Y combinator.
Wednesday, August 22, 12

Here it is. Thank you, good night and good luck.
(defn Y
                   [g]
                   ((fn [x] (x x)) (fn [x]
                                    (g (fn [y] ((x x) y))))))

                 (defn almost-factorial
                   [f]
                   (fn [n]
                     (if (= n 0)
                       1
                       (* n (f (dec n))))))

                 (defn factorial
                   [n]
                   ((Y almost-factorial) n))


Wednesday, August 22, 12

Here it is. Thank you, good night and good luck.
Questions?



Wednesday, August 22, 12
An example: 5!
                           (factorial 5) = 5 * 4 * 3 * 2 * 1 = 120



                           (defn factorial
                             [n]
                             (if (= n 0)
                               1
                               (* n (factorial (dec n)))))




Wednesday, August 22, 12

Let’s back up. This is how a sane person would define factorial... recursively.
here to here?

       (defn factorial                   (defn almost-factorial
         [n]                               [f]
         (if (= n 0)                       (fn [n]
           1                                 (if (= n 0)
           (* n (factorial (dec n)))))         1
                                               (* n (f (dec n))))))

                                         (defn Y
                                           [g]
                                           ((fn [x] (x x)) (fn [x]
                                                            (g (fn [y] ((x x) y))))))

                                         (defn factorial
                                           [n]
                                           ((Y almost-factorial) n))



Wednesday, August 22, 12

Recursive definition to non-recursive
2 Things

                           1) recursion

                           2) functions



Wednesday, August 22, 12
2 Things

                           1) recursion
                           2) functions



Wednesday, August 22, 12
“The Y combinator allows recursion
                                                     recursion...
                                as a set of rewrite rules,
                            without requiring native recursion
                                support in the language.”

                                        -- Someone on Wikipedia




Wednesday, August 22, 12
replace
                           “native recursion”
                                  with
                           manual recursion


Wednesday, August 22, 12
(defn factorial
           [n]                               (defn fact
           (if (= n 0)                         [n]
             1                                 (if (= n 0)
               (* n (factorial (dec n)))))       1
                                                 (* n (ERROR (dec n)))))




Wednesday, August 22, 12
(defn factorial
           [n]                               (defn fact
           (if (= n 0)                         [n]
             1                                 (if (= n 0)
               (* n (factorial (dec n)))))       1
                                                 (* n (ERROR (dec n)))))




                    n = 0 OK

                    n = 1 BOOM!




Wednesday, August 22, 12
(defn factorial
           [n]                               (defn fact                    (defn fact
           (if (= n 0)                         [n]                           [n]
             1                                 (if (= n 0)                   (if (= n 0)
               (* n (factorial (dec n)))))       1                             1
                                                 (* n (ERROR (dec n)))))       (* n (ERROR (dec n)


                                             (defn fact                    (defn fact
                                               [n]                           [n]
                                               (if (= n 0)                   (if (= n 0)
                                                 1                             1
                    n = 0 OK                     (* n (ERROR (dec n)))))       (* n (ERROR (dec n))


                                             (defn fact                    (defn fact
                                               [n]                           [n]
                    n = 1 BOOM!                (if (= n 0)                   (if (= n 0)
                                                 1                             1
                                                 (* n (ERROR (dec n)))))       (* n (ERROR (dec n)


                                             (defn fact                    (defn fact
                                               [n]                           [n]
                                               (if (= n 0)                   (if (= n 0)
                                                 1                             1
                                                 (* n (ERROR (dec n)))))       (* n (ERROR (dec n)

Wednesday, August 22, 12
(defn fact
                                                       (fn [n]                             (fn [n]
                   [n]
                                                         (if (= n 0)                         (if (= n
                   (if (= n 0)
                                                           1                                   1
                     1
                                                           (* n (ERROR (dec n)))))             (* n (
                      (* n (ERROR (dec n)))))


                                   (fn [n]                              (fn [n]
                                     (if (= n 0)                          (if (= n 0)
                                       1                                    1
                                       (* n (ERROR (dec n)))))              (* n (ERROR (dec n)))))




Wednesday, August 22, 12
(defn fact
                                                       (fn [n]                             (fn [n]
                   [n]
                                                         (if (= n 0)                         (if (= n
                   (if (= n 0)
                                                           1                                   1
                     1
                                                           (* n (ERROR (dec n)))))             (* n (
                      (* n (ERROR (dec n)))))


                                   (fn [n]                              (fn [n]
                                     (if (= n 0)                          (if (= n 0)
                                       1                                    1
                                       (* n (ERROR (dec n)))))              (* n (ERROR (dec n)))))




            n=0               n=1            n=2          n=3            n=4

Wednesday, August 22, 12
replace
                           “native recursion”
                                  with
                           manual recursion
                            “rewrite rules”

Wednesday, August 22, 12
2 Things

                           1) recursion

                           2) functions



Wednesday, August 22, 12
2 Things

                           1) recursion

                           2) functions

Wednesday, August 22, 12
Functions are machines.

                           Functions are relationships,
                           between inputs and outputs.




Wednesday, August 22, 12
A function is a blender.




Wednesday, August 22, 12
FIRST ORDER BLENDER
                           A normal blender that consumes single input
                                                   and creates output.




Wednesday, August 22, 12
FIRST ORDER BLENDER
                           A normal blender that consumes single input
                                                   and creates output.



       HIGHER ORDER BLENDER
                              A special blender that consumes a blender
                                            and outputs another blender.




Wednesday, August 22, 12
FIRST ORDER BLENDER
                           A normal blender that consumes single input
                                                   and creates output.



       HIGHER ORDER BLENDER
                              A special blender that consumes a blender
                                            and outputs another blender.



     FIXPOINT (BLENDER) COMBINATOR                                  Y
         Consumes a blender and produces a new blender that
                         can consume any number of inputs.

Wednesday, August 22, 12
ONE




Wednesday, August 22, 12
ONE




    (defn almost-factorial
       [f]
       (fn [n]
         (if (= n 0)
              1
              (* n (f (dec n))))))




Wednesday, August 22, 12
ONE      ANY




    (defn almost-factorial
       [f]
       (fn [n]
         (if (= n 0)
              1
              (* n (f (dec n))))))




Wednesday, August 22, 12
ONE          ANY




                                     Y


    (defn almost-factorial
       [f]                               factorial
       (fn [n]
         (if (= n 0)
              1
              (* n (f (dec n))))))




Wednesday, August 22, 12
if you squint

       (defn factorial                   (defn almost-factorial
         [n]                               [f]
         (if (= n 0)                       (fn [n]
           1                                 (if (= n 0)
           (* n (factorial (dec n)))))         1
                                               (* n (f (dec n))))))

                                         (defn Y
                                           [g]
                                           ((fn [x] (x x)) (fn [x]
                                                            (g (fn [y] ((x x) y))))))

                                         (defn factorial
                                           [n]
                                           ((Y almost-factorial) n))



Wednesday, August 22, 12

Derivation not possible in 3 minutes.
I’m so sorry.


 No kittens were blended during the creation of this presentation.
Wednesday, August 22, 12
No really, done now.


 No kittens were blended during the creation of this presentation.
Wednesday, August 22, 12
A Clojure Primer
     PARENTHESIS
                           (+ 1 2 3)
                           ;; => 6

     PREFIX NOTATION
                           (operator arg1 arg2 arg3)


     FUNCTIONS
                           (defn multby2               (fn [n] (* n 2))
                             [n]
                             (* n 2))
                           ;; (multby2 4) => 8

Wednesday, August 22, 12

1) First, a primer on LISP & Clojure
- parens for function call
- prefix notation, followed by arguments
2) And, function definition and anonymous functions
(defn simple-factorial
                    [n]
                    (if (= n 0)
                      1
                      (* n (simple-factorial (dec n)))))




Wednesday, August 22, 12

Here, we remove the recursive definition. Kind of delaying it, for now.
(defn simple-factorial
                    [n]
                    (if (= n 0)
                      1
                      (* n (simple-factorial (dec n)))))



                   (defn part
                     [self n]
                     (if (= n 0)
                       1
                       (* n (self self (dec n)))))
                   ;; (part part 5) => 120




Wednesday, August 22, 12

Here, we remove the recursive definition. Kind of delaying it, for now.
(defn simple-factorial
                    [n]
                    (if (= n 0)
                      1
                      (* n (simple-factorial (dec n)))))



                   (defn part
                     [self n]
                     (if (= n 0)
                       1
                       (* n (self self (dec n)))))
                   ;; (part part 5) => 120




Wednesday, August 22, 12

Change part to take a single arg, returning a function that takes n.
(defn part
                     [self n]
                     (if (= n 0)
                       1
                       (* n (self self (dec n)))))
                   ;; (part part 5) => 120


                   (defn part2
                     [self]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n ((self self) (dec n))))))
                   ;; ((part2 part2) 5) => 120



Wednesday, August 22, 12

Change part to take a single arg, returning a function that takes n.
(defn part
                     [self n]
                     (if (= n 0)
                       1
                       (* n (self self (dec n)))))
                   ;; (part part 5) => 120


                   (defn part2
                     [self]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n ((self self) (dec n))))))
                   ;; ((part2 part2) 5) => 120



Wednesday, August 22, 12

Replace (self self) with f, which blows up in a Stack Overflow... but, we press on.
(defn part2
                     [self]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n ((self self) (dec n))))))
                   ;; ((part2 part2) 5) => 120


                   (defn part3
                     [self]
                     (let [f (self self)]
                       (fn [n]
                         (if (= n 0)
                            1
                            (* n (f (dec n)))))))


Wednesday, August 22, 12

Replace (self self) with f, which blows up in a Stack Overflow... but, we press on.
(defn part2
                     [self]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n ((self self) (dec n))))))
                   ;; ((part2 part2) 5) => 120


                   (defn part3
                     [self]
                     (let [f (self self)]
                       (fn [n]
                         (if (= n 0)
                            1
                            (* n (f (dec n)))))))


Wednesday, August 22, 12

Bury, the (self self) call in a lambda.
(defn part3
                     [self]
                     (let [f (self self)]
                       (fn [n]
                         (if (= n 0)
                            1
                            (* n (f (dec n)))))))


                   (defn part4
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (fn [n]
                        (if (= n 0)
                          1
                          (* n (f (dec n)))))))


Wednesday, August 22, 12

Bury, the (self self) call in a lambda.
(defn part3
                     [self]
                     (let [f (self self)]
                       (fn [n]
                         (if (= n 0)
                            1
                            (* n (f (dec n)))))))


                   (defn part4
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (fn [n]
                        (if (= n 0)
                          1
                          (* n (f (dec n)))))))


Wednesday, August 22, 12

Rip out the function that looks almost like the factorial function. This is what we’re
generalizing. The Y combinator computes the fixpoint of this function.
(defn part4
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (fn [n]
                        (if (= n 0)
                          1
                          (* n (f (dec n)))))))


                   (defn almost-factorial
                     [f]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n (f (dec n))))))



Wednesday, August 22, 12

Rip out the function that looks almost like the factorial function. This is what we’re
generalizing. The Y combinator computes the fixpoint of this function.
(defn part4
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (fn [n]
                        (if (= n 0)
                          1
                          (* n (f (dec n)))))))


                   (defn almost-factorial
                     [f]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n (f (dec n))))))



Wednesday, August 22, 12

Insert almost-factorial into the part function.
(defn almost-factorial
                     [f]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n (f (dec n))))))



                   (defn part5
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (almost-factorial f)))




Wednesday, August 22, 12

Insert almost-factorial into the part function.
(defn almost-factorial
                     [f]
                     (fn [n]
                       (if (= n 0)
                         1
                         (* n (f (dec n))))))



                   (defn part5
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (almost-factorial f)))




Wednesday, August 22, 12

fact5 is a working factorial function, but we can generalize it
(defn part5
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (almost-factorial f)))




                   (defn fact5
                     [n]
                     ((part5 part5) n))




Wednesday, August 22, 12

fact5 is a working factorial function, but we can generalize it
(defn part5
                     [self]
                     (let [f (fn [y] ((self self) y))]
                       (almost-factorial f)))




                   (defn fact5
                     [n]
                     ((part5 part5) n))




Wednesday, August 22, 12

here we embed the definition the “part” function
(defn fact5
                     [n]
                     ((part5 part5) n))




                   (def fact6
                     (let [part (fn [self]
                                  (let [f (fn [y] ((self self) y))]
                                     (almost-factorial f)))]
                       (part part)))




Wednesday, August 22, 12

here we embed the definition the “part” function
(defn fact5
                     [n]
                     ((part5 part5) n))




                   (def fact6
                     (let [part (fn [self]
                                  (let [f (fn [y] ((self self) y))]
                                     (almost-factorial f)))]
                       (part part)))




Wednesday, August 22, 12

rename part => x
and self => x
for kicks really
(def fact6
                     (let [part (fn [self]
                                  (let [f (fn [y] ((self self) y))]
                                     (almost-factorial f)))]
                       (part part)))




                   (def fact7
                     (let [x (fn [x]
                               (let [f (fn [y] ((x x) y))]
                                 (almost-factorial f)))]
                       (x x)))




Wednesday, August 22, 12

rename part => x
and self => x
for kicks really
(def fact6
                       (let [part (fn [self]
                                    (let [f (fn [y] ((self self) y))]
                                       (almost-factorial f)))]
                         (part part)))




                   (def fact7
                     (let [x (fn [x]
                               (let [f (fn [y] ((x x) y))]
                                 (almost-factorial f)))]
                       (x x)))




Wednesday, August 22, 12

replace the (x x) invocation with a lambda of the same
(def fact7
                     (let [x (fn [x]
                               (let [f (fn [y] ((x x) y))]
                                 (almost-factorial f)))]
                       (x x)))




                   (def fact8
                     ((fn [x]
                        (x x)) (fn [x]
                                 (let [f (fn [y] ((x x) y))]
                                   (almost-factorial f)))))




Wednesday, August 22, 12

replace the (x x) invocation with a lambda of the same
(def fact7
                     (let [x (fn [x]
                               (let [f (fn [y] ((x x) y))]
                                 (almost-factorial f)))]
                       (x x)))




                   (def fact8
                     ((fn [x]
                        (x x)) (fn [x]
                                 (let [f (fn [y] ((x x) y))]
                                   (almost-factorial f)))))




Wednesday, August 22, 12

Rename to Y
and generalize, by accepting a function g and using this to replace almost-factorial
(def fact8
                     ((fn [x]
                        (x x)) (fn [x]
                                 (let [f (fn [y] ((x x) y))]
                                   (almost-factorial f)))))




                   (defn nearly-Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (let [f (fn [y] ((x x) y))]
                                        (g f)))))




Wednesday, August 22, 12

Rename to Y
and generalize, by accepting a function g and using this to replace almost-factorial
(def fact8
                     ((fn [x]
                        (x x)) (fn [x]
                                 (let [f (fn [y] ((x x) y))]
                                   (almost-factorial f)))))




                   (defn nearly-Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (let [f (fn [y] ((x x) y))]
                                        (g f)))))




Wednesday, August 22, 12

Replace f with the anonymous function bound to it
(defn nearly-Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (let [f (fn [y] ((x x) y))]
                                        (g f)))))




                   (defn Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (g (fn [y] ((x x) y))))))




Wednesday, August 22, 12

Replace f with the anonymous function bound to it
(defn nearly-Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (let [f (fn [y] ((x x) y))]
                                        (g f)))))




                   (defn Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (g (fn [y] ((x x) y))))))




Wednesday, August 22, 12
(defn Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (g (fn [y] ((x x) y))))))




                   (defn factorial
                     [n]
                     ((Y almost-factorial) n))




Wednesday, August 22, 12
(defn Y
                     [g]
                     ((fn [x] (x x)) (fn [x]
                                      (g (fn [y] ((x x) y))))))




                            I’m so sorry.
                   (defn factorial
                     [n]
                     ((Y almost-factorial) n))




Wednesday, August 22, 12
I’m so sorry.


 No kittens were blended during the creation of this presentation.
Wednesday, August 22, 12
(defn almost-factorial            (Y almost-factorial)
         [f]
         (fn [n]
                                       ;; ((Y almost-factorial) 5) => 120
           (if (= n 0)
                1
                (* n (f (dec n))))))




Wednesday, August 22, 12
(defn almost-factorial            (Y almost-factorial)
         [f]
         (fn [n]
                                       ;; ((Y almost-factorial) 5) => 120
           (if (= n 0)
                1
                (* n (f (dec n))))))




             Y




Wednesday, August 22, 12
(defn almost-factorial            (Y almost-factorial)
         [f]
         (fn [n]
                                       ;; ((Y almost-factorial) 5) => 120
           (if (= n 0)
                1
                (* n (f (dec n))))))




             Y




    FACTORIAL
Wednesday, August 22, 12
(defn almost-factorial                 (Y almost-factorial)
         [f]
         (fn [n]
                                            ;; ((Y almost-factorial) 5) => 120
           (if (= n 0)
                1
                (* n (f (dec n))))))




                            (defn fact                            (defn fact
                              [n]                                   [n]
                              (if (= n 0)                           (if (= n 0)

             Y
                                1                                     1
                                (* n (ERROR (dec n)))))               (* n (ERROR (dec n)))))


                                              (defn fact                           (defn fact
                                                [n]                                  [n]
                                                (if (= n 0)                          (if (= n 0)
                                                  1                                    1
                                                  (* n (ERROR (dec n)))))              (* n (ERROR (d




    FACTORIAL
Wednesday, August 22, 12
(defn almost-factorial
        [f]
        (fn [n]
          (if (= n 0)
                1
                (* n (f (dec n))))))




                           Y




            factorial

Wednesday, August 22, 12
(defn almost-factorial
        [f]
        (fn [n]
          (if (= n 0)
                1
                (* n (f (dec n))))))




                           Y




            factorial

Wednesday, August 22, 12
(defn almost-factorial
        [f]
        (fn [n]
          (if (= n 0)
                1                      ONE
                (* n (f (dec n))))))




                           Y




            factorial

Wednesday, August 22, 12
(defn almost-factorial
        [f]
        (fn [n]
          (if (= n 0)                        ANY
                1                      ONE
                (* n (f (dec n))))))




                           Y




            factorial

Wednesday, August 22, 12

Contenu connexe

En vedette

Javascript call ObjC
Javascript call ObjCJavascript call ObjC
Javascript call ObjCLin Luxiang
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtimeInferis
 
Working with AFNetworking
Working with AFNetworkingWorking with AFNetworking
Working with AFNetworkingwaynehartman
 
Jak nastartovat vývojový projekt
Jak nastartovat vývojový projektJak nastartovat vývojový projekt
Jak nastartovat vývojový projektJiří Knesl
 
Writing readable Clojure code
Writing readable Clojure codeWriting readable Clojure code
Writing readable Clojure codeJiří Knesl
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?Kyle Oba
 
Core Data with multiple managed object contexts
Core Data with multiple managed object contextsCore Data with multiple managed object contexts
Core Data with multiple managed object contextsMatthew Morey
 

En vedette (7)

Javascript call ObjC
Javascript call ObjCJavascript call ObjC
Javascript call ObjC
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
 
Working with AFNetworking
Working with AFNetworkingWorking with AFNetworking
Working with AFNetworking
 
Jak nastartovat vývojový projekt
Jak nastartovat vývojový projektJak nastartovat vývojový projekt
Jak nastartovat vývojový projekt
 
Writing readable Clojure code
Writing readable Clojure codeWriting readable Clojure code
Writing readable Clojure code
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Core Data with multiple managed object contexts
Core Data with multiple managed object contextsCore Data with multiple managed object contexts
Core Data with multiple managed object contexts
 

Similaire à OUDL Y Combinator

Question bank unit ii engineering mathematics ii
Question bank unit ii engineering mathematics iiQuestion bank unit ii engineering mathematics ii
Question bank unit ii engineering mathematics iiShubham Vini
 
Lesson 19: Double Integrals over General Regions
Lesson 19: Double Integrals over General RegionsLesson 19: Double Integrals over General Regions
Lesson 19: Double Integrals over General RegionsMatthew Leingang
 
Lesson 29: Integration by Substition
Lesson 29: Integration by SubstitionLesson 29: Integration by Substition
Lesson 29: Integration by SubstitionMatthew Leingang
 
X2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsX2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsNigel Simmons
 
Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Matthew Leingang
 
Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Mel Anthony Pepito
 
Lesson 4 Nov 17 09
Lesson 4 Nov 17 09Lesson 4 Nov 17 09
Lesson 4 Nov 17 09ingroy
 
Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)Matthew Leingang
 

Similaire à OUDL Y Combinator (10)

Question bank unit ii engineering mathematics ii
Question bank unit ii engineering mathematics iiQuestion bank unit ii engineering mathematics ii
Question bank unit ii engineering mathematics ii
 
Plug-and-Play methods for inverse problems in imagine, by Julie Delon
Plug-and-Play methods for inverse problems in imagine, by Julie DelonPlug-and-Play methods for inverse problems in imagine, by Julie Delon
Plug-and-Play methods for inverse problems in imagine, by Julie Delon
 
Lesson 19: Double Integrals over General Regions
Lesson 19: Double Integrals over General RegionsLesson 19: Double Integrals over General Regions
Lesson 19: Double Integrals over General Regions
 
7.4 composition
7.4 composition7.4 composition
7.4 composition
 
Lesson 29: Integration by Substition
Lesson 29: Integration by SubstitionLesson 29: Integration by Substition
Lesson 29: Integration by Substition
 
X2 T05 06 Partial Fractions
X2 T05 06 Partial FractionsX2 T05 06 Partial Fractions
X2 T05 06 Partial Fractions
 
Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)
 
Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)Lesson 27: Integration by Substitution (Section 041 slides)
Lesson 27: Integration by Substitution (Section 041 slides)
 
Lesson 4 Nov 17 09
Lesson 4 Nov 17 09Lesson 4 Nov 17 09
Lesson 4 Nov 17 09
 
Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)Lesson 29: Integration by Substition (worksheet solutions)
Lesson 29: Integration by Substition (worksheet solutions)
 

Dernier

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Dernier (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

OUDL Y Combinator

  • 1. We are OUDL. Organization for the Understanding of Dynamic Languages http://meetup.com/dynamic/ Wednesday, August 22, 12 We are programming language enthusiasts Check us out on Meetup.com All events have been by members, for members Each event has a theme, a selected pastry or baked good, and a horrible logo
  • 2. What makes Objective C dynamic? Wednesday, August 22, 12
  • 3. What makes Objective C dynamic? Kamehameha Bakery donuts Otto Cake cheesecake Cake Couture cupcakes Fendu Bakery croissants & cookies Saint Germain Bakery palmiers Wednesday, August 22, 12
  • 5. Y combinator Examples in Clojure. Also includes: blenders and kittens. Caveat emptor: I make no effort to teach you Clojure. Kyle Oba @mudphone Pas de Chocolat Wednesday, August 22, 12
  • 6. Not this one. Wednesday, August 22, 12 Paul Graham did name his company after the *REAL* Y combinator. But, why?
  • 7. This one. (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) Wednesday, August 22, 12 Which is this thing, in Clojure.
  • 8. Let’s get started. Here’s a non-recursive definition of factorial, using the Y combinator. Wednesday, August 22, 12 Here it is. Thank you, good night and good luck.
  • 9. (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) (defn factorial [n] ((Y almost-factorial) n)) Wednesday, August 22, 12 Here it is. Thank you, good night and good luck.
  • 11. An example: 5! (factorial 5) = 5 * 4 * 3 * 2 * 1 = 120 (defn factorial [n] (if (= n 0) 1 (* n (factorial (dec n))))) Wednesday, August 22, 12 Let’s back up. This is how a sane person would define factorial... recursively.
  • 12. here to here? (defn factorial (defn almost-factorial [n] [f] (if (= n 0) (fn [n] 1 (if (= n 0) (* n (factorial (dec n))))) 1 (* n (f (dec n)))))) (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) (defn factorial [n] ((Y almost-factorial) n)) Wednesday, August 22, 12 Recursive definition to non-recursive
  • 13. 2 Things 1) recursion 2) functions Wednesday, August 22, 12
  • 14. 2 Things 1) recursion 2) functions Wednesday, August 22, 12
  • 15. “The Y combinator allows recursion recursion... as a set of rewrite rules, without requiring native recursion support in the language.” -- Someone on Wikipedia Wednesday, August 22, 12
  • 16. replace “native recursion” with manual recursion Wednesday, August 22, 12
  • 17. (defn factorial [n] (defn fact (if (= n 0) [n] 1 (if (= n 0) (* n (factorial (dec n))))) 1 (* n (ERROR (dec n))))) Wednesday, August 22, 12
  • 18. (defn factorial [n] (defn fact (if (= n 0) [n] 1 (if (= n 0) (* n (factorial (dec n))))) 1 (* n (ERROR (dec n))))) n = 0 OK n = 1 BOOM! Wednesday, August 22, 12
  • 19. (defn factorial [n] (defn fact (defn fact (if (= n 0) [n] [n] 1 (if (= n 0) (if (= n 0) (* n (factorial (dec n))))) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n) (defn fact (defn fact [n] [n] (if (= n 0) (if (= n 0) 1 1 n = 0 OK (* n (ERROR (dec n))))) (* n (ERROR (dec n)) (defn fact (defn fact [n] [n] n = 1 BOOM! (if (= n 0) (if (= n 0) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n) (defn fact (defn fact [n] [n] (if (= n 0) (if (= n 0) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n) Wednesday, August 22, 12
  • 20. (defn fact (fn [n] (fn [n] [n] (if (= n 0) (if (= n (if (= n 0) 1 1 1 (* n (ERROR (dec n))))) (* n ( (* n (ERROR (dec n))))) (fn [n] (fn [n] (if (= n 0) (if (= n 0) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n))))) Wednesday, August 22, 12
  • 21. (defn fact (fn [n] (fn [n] [n] (if (= n 0) (if (= n (if (= n 0) 1 1 1 (* n (ERROR (dec n))))) (* n ( (* n (ERROR (dec n))))) (fn [n] (fn [n] (if (= n 0) (if (= n 0) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n))))) n=0 n=1 n=2 n=3 n=4 Wednesday, August 22, 12
  • 22. replace “native recursion” with manual recursion “rewrite rules” Wednesday, August 22, 12
  • 23. 2 Things 1) recursion 2) functions Wednesday, August 22, 12
  • 24. 2 Things 1) recursion 2) functions Wednesday, August 22, 12
  • 25. Functions are machines. Functions are relationships, between inputs and outputs. Wednesday, August 22, 12
  • 26. A function is a blender. Wednesday, August 22, 12
  • 27. FIRST ORDER BLENDER A normal blender that consumes single input and creates output. Wednesday, August 22, 12
  • 28. FIRST ORDER BLENDER A normal blender that consumes single input and creates output. HIGHER ORDER BLENDER A special blender that consumes a blender and outputs another blender. Wednesday, August 22, 12
  • 29. FIRST ORDER BLENDER A normal blender that consumes single input and creates output. HIGHER ORDER BLENDER A special blender that consumes a blender and outputs another blender. FIXPOINT (BLENDER) COMBINATOR Y Consumes a blender and produces a new blender that can consume any number of inputs. Wednesday, August 22, 12
  • 31. ONE (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12
  • 32. ONE ANY (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12
  • 33. ONE ANY Y (defn almost-factorial [f] factorial (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12
  • 34. if you squint (defn factorial (defn almost-factorial [n] [f] (if (= n 0) (fn [n] 1 (if (= n 0) (* n (factorial (dec n))))) 1 (* n (f (dec n)))))) (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) (defn factorial [n] ((Y almost-factorial) n)) Wednesday, August 22, 12 Derivation not possible in 3 minutes.
  • 35. I’m so sorry. No kittens were blended during the creation of this presentation. Wednesday, August 22, 12
  • 36. No really, done now. No kittens were blended during the creation of this presentation. Wednesday, August 22, 12
  • 37. A Clojure Primer PARENTHESIS (+ 1 2 3) ;; => 6 PREFIX NOTATION (operator arg1 arg2 arg3) FUNCTIONS (defn multby2 (fn [n] (* n 2)) [n] (* n 2)) ;; (multby2 4) => 8 Wednesday, August 22, 12 1) First, a primer on LISP & Clojure - parens for function call - prefix notation, followed by arguments 2) And, function definition and anonymous functions
  • 38. (defn simple-factorial [n] (if (= n 0) 1 (* n (simple-factorial (dec n))))) Wednesday, August 22, 12 Here, we remove the recursive definition. Kind of delaying it, for now.
  • 39. (defn simple-factorial [n] (if (= n 0) 1 (* n (simple-factorial (dec n))))) (defn part [self n] (if (= n 0) 1 (* n (self self (dec n))))) ;; (part part 5) => 120 Wednesday, August 22, 12 Here, we remove the recursive definition. Kind of delaying it, for now.
  • 40. (defn simple-factorial [n] (if (= n 0) 1 (* n (simple-factorial (dec n))))) (defn part [self n] (if (= n 0) 1 (* n (self self (dec n))))) ;; (part part 5) => 120 Wednesday, August 22, 12 Change part to take a single arg, returning a function that takes n.
  • 41. (defn part [self n] (if (= n 0) 1 (* n (self self (dec n))))) ;; (part part 5) => 120 (defn part2 [self] (fn [n] (if (= n 0) 1 (* n ((self self) (dec n)))))) ;; ((part2 part2) 5) => 120 Wednesday, August 22, 12 Change part to take a single arg, returning a function that takes n.
  • 42. (defn part [self n] (if (= n 0) 1 (* n (self self (dec n))))) ;; (part part 5) => 120 (defn part2 [self] (fn [n] (if (= n 0) 1 (* n ((self self) (dec n)))))) ;; ((part2 part2) 5) => 120 Wednesday, August 22, 12 Replace (self self) with f, which blows up in a Stack Overflow... but, we press on.
  • 43. (defn part2 [self] (fn [n] (if (= n 0) 1 (* n ((self self) (dec n)))))) ;; ((part2 part2) 5) => 120 (defn part3 [self] (let [f (self self)] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) Wednesday, August 22, 12 Replace (self self) with f, which blows up in a Stack Overflow... but, we press on.
  • 44. (defn part2 [self] (fn [n] (if (= n 0) 1 (* n ((self self) (dec n)))))) ;; ((part2 part2) 5) => 120 (defn part3 [self] (let [f (self self)] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) Wednesday, August 22, 12 Bury, the (self self) call in a lambda.
  • 45. (defn part3 [self] (let [f (self self)] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) (defn part4 [self] (let [f (fn [y] ((self self) y))] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) Wednesday, August 22, 12 Bury, the (self self) call in a lambda.
  • 46. (defn part3 [self] (let [f (self self)] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) (defn part4 [self] (let [f (fn [y] ((self self) y))] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) Wednesday, August 22, 12 Rip out the function that looks almost like the factorial function. This is what we’re generalizing. The Y combinator computes the fixpoint of this function.
  • 47. (defn part4 [self] (let [f (fn [y] ((self self) y))] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12 Rip out the function that looks almost like the factorial function. This is what we’re generalizing. The Y combinator computes the fixpoint of this function.
  • 48. (defn part4 [self] (let [f (fn [y] ((self self) y))] (fn [n] (if (= n 0) 1 (* n (f (dec n))))))) (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12 Insert almost-factorial into the part function.
  • 49. (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) (defn part5 [self] (let [f (fn [y] ((self self) y))] (almost-factorial f))) Wednesday, August 22, 12 Insert almost-factorial into the part function.
  • 50. (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) (defn part5 [self] (let [f (fn [y] ((self self) y))] (almost-factorial f))) Wednesday, August 22, 12 fact5 is a working factorial function, but we can generalize it
  • 51. (defn part5 [self] (let [f (fn [y] ((self self) y))] (almost-factorial f))) (defn fact5 [n] ((part5 part5) n)) Wednesday, August 22, 12 fact5 is a working factorial function, but we can generalize it
  • 52. (defn part5 [self] (let [f (fn [y] ((self self) y))] (almost-factorial f))) (defn fact5 [n] ((part5 part5) n)) Wednesday, August 22, 12 here we embed the definition the “part” function
  • 53. (defn fact5 [n] ((part5 part5) n)) (def fact6 (let [part (fn [self] (let [f (fn [y] ((self self) y))] (almost-factorial f)))] (part part))) Wednesday, August 22, 12 here we embed the definition the “part” function
  • 54. (defn fact5 [n] ((part5 part5) n)) (def fact6 (let [part (fn [self] (let [f (fn [y] ((self self) y))] (almost-factorial f)))] (part part))) Wednesday, August 22, 12 rename part => x and self => x for kicks really
  • 55. (def fact6 (let [part (fn [self] (let [f (fn [y] ((self self) y))] (almost-factorial f)))] (part part))) (def fact7 (let [x (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f)))] (x x))) Wednesday, August 22, 12 rename part => x and self => x for kicks really
  • 56. (def fact6 (let [part (fn [self] (let [f (fn [y] ((self self) y))] (almost-factorial f)))] (part part))) (def fact7 (let [x (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f)))] (x x))) Wednesday, August 22, 12 replace the (x x) invocation with a lambda of the same
  • 57. (def fact7 (let [x (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f)))] (x x))) (def fact8 ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f))))) Wednesday, August 22, 12 replace the (x x) invocation with a lambda of the same
  • 58. (def fact7 (let [x (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f)))] (x x))) (def fact8 ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f))))) Wednesday, August 22, 12 Rename to Y and generalize, by accepting a function g and using this to replace almost-factorial
  • 59. (def fact8 ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f))))) (defn nearly-Y [g] ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (g f))))) Wednesday, August 22, 12 Rename to Y and generalize, by accepting a function g and using this to replace almost-factorial
  • 60. (def fact8 ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (almost-factorial f))))) (defn nearly-Y [g] ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (g f))))) Wednesday, August 22, 12 Replace f with the anonymous function bound to it
  • 61. (defn nearly-Y [g] ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (g f))))) (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) Wednesday, August 22, 12 Replace f with the anonymous function bound to it
  • 62. (defn nearly-Y [g] ((fn [x] (x x)) (fn [x] (let [f (fn [y] ((x x) y))] (g f))))) (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) Wednesday, August 22, 12
  • 63. (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) (defn factorial [n] ((Y almost-factorial) n)) Wednesday, August 22, 12
  • 64. (defn Y [g] ((fn [x] (x x)) (fn [x] (g (fn [y] ((x x) y)))))) I’m so sorry. (defn factorial [n] ((Y almost-factorial) n)) Wednesday, August 22, 12
  • 65. I’m so sorry. No kittens were blended during the creation of this presentation. Wednesday, August 22, 12
  • 66. (defn almost-factorial (Y almost-factorial) [f] (fn [n] ;; ((Y almost-factorial) 5) => 120 (if (= n 0) 1 (* n (f (dec n)))))) Wednesday, August 22, 12
  • 67. (defn almost-factorial (Y almost-factorial) [f] (fn [n] ;; ((Y almost-factorial) 5) => 120 (if (= n 0) 1 (* n (f (dec n)))))) Y Wednesday, August 22, 12
  • 68. (defn almost-factorial (Y almost-factorial) [f] (fn [n] ;; ((Y almost-factorial) 5) => 120 (if (= n 0) 1 (* n (f (dec n)))))) Y FACTORIAL Wednesday, August 22, 12
  • 69. (defn almost-factorial (Y almost-factorial) [f] (fn [n] ;; ((Y almost-factorial) 5) => 120 (if (= n 0) 1 (* n (f (dec n)))))) (defn fact (defn fact [n] [n] (if (= n 0) (if (= n 0) Y 1 1 (* n (ERROR (dec n))))) (* n (ERROR (dec n))))) (defn fact (defn fact [n] [n] (if (= n 0) (if (= n 0) 1 1 (* n (ERROR (dec n))))) (* n (ERROR (d FACTORIAL Wednesday, August 22, 12
  • 70. (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Y factorial Wednesday, August 22, 12
  • 71. (defn almost-factorial [f] (fn [n] (if (= n 0) 1 (* n (f (dec n)))))) Y factorial Wednesday, August 22, 12
  • 72. (defn almost-factorial [f] (fn [n] (if (= n 0) 1 ONE (* n (f (dec n)))))) Y factorial Wednesday, August 22, 12
  • 73. (defn almost-factorial [f] (fn [n] (if (= n 0) ANY 1 ONE (* n (f (dec n)))))) Y factorial Wednesday, August 22, 12