SlideShare a Scribd company logo
How to Reinvent
the Y combinator
    Yin Wang
Why do we need the Y combinator?


      For fundamental understanding of
      recursion, we hope to create
      recursive functions using the
      lambda calculus:

                x | t t | λx.t
Why not define or letrec?
(define length                             (letrec ([length
 (lambda (ls)                                     (lambda (ls)
   (cond                                            (cond
    [(null? ls) 0]                                   [(null? ls) 0]
    [else (add1 (length (cdr ls)))])))               [else (add1 (length (cdr ls)))]))])
                                             (length '(a b c)))

             1. We don’t have define or letrec in lambda calculus

             2. For fundamental understanding of recursion, we
                want to see how define and letrec can be created
                using just the three elements of lambda calculus:
                                 x | t t | λx.t

             3. You will see how this understanding can be useful
                when constructing compilers
Plan
(define length
 (lambda (ls)
   (cond
    [(null? ls) 0]
    [else (add1 (length (cdr ls)))])))


• We start by constructing a recursive definition of
  “length” in a pure subset of Scheme
• Then extract a common pattern that can be
  applied to recursive definitions in general
• This common pattern is the Y combinator
How do we do that?

• First, notice we can’t really define a recursive
  function without binding it to a name

• Second, answer this question:
  “Where can we bind something to a name?”

• The answer is: λx.t

• Lambda, the ultimate binder
Step 1: Binder
 (define length
  (lambda (ls)
    (cond
     [(null? ls) 0]
     [else (add1 (length (cdr ls)))])))




• Step1: create a lambda similar to this define
• This creates a binder where we can bind the function to
• Our goal: bind the function “length” to the name length
Step 1: Binder
 (lambda (length)
   (lambda (ls)
     (cond
      [(null? ls) 0]
      [else (add1 (length (cdr ls)))])))




• Step1: create a lambda similar to this define
• This creates a binder where we can bind the function to
• Our goal: bind the function “length” to the name length
Step 2: Copy
((lambda (length)
  (lambda (ls)
    (cond
     [(null? ls) 0]
     [else (add1 (length (cdr ls)))])))
(lambda (length)
  (lambda (ls)
    (cond
     [(null? ls) 0]
     [else (add1 (length (cdr ls)))]))))


         • Make a copy of the function and apply itself
           to the copy (self-application)
         • This will successfully bind the name length
           to the function “itself”
Step 3: Small fix
((lambda (length)
  (lambda (ls)
    (cond
     [(null? ls) 0]
     [else (add1 ((length length) (cdr ls)))])))
(lambda (length)
  (lambda (ls)
    (cond
     [(null? ls) 0]
     [else (add1 ((length length) (cdr ls)))]))))



     The first argument to the application of
     “length” should be itself
Step 4: Extract Patterns
((lambda (length)
   (lambda (ls)
     (cond
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))])))
 (lambda (length)
   (lambda (ls)
     (cond
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))


  • This recursive function will work (try it!)
  • This is called “poor man’s Y”
  • Now we are going to extract the pattern
    in there, so that the same pattern works
    for any function.
Step 4: Extract Patterns
((lambda (length)
   (lambda (ls)
     (cond                                           • But we can’t see the original
      [(null? ls) 0]                                   definition in there.
      [else (add1 ((length length) (cdr ls)))])))    • We hope to see this, but the
 (lambda (length)                                      self-applications (length length)
   (lambda (ls)                                        bother us.
     (cond                                           • Hope we can get rid of them
      [(null? ls) 0]
                                                       while preserving the semantics.
      [else (add1 ((length length) (cdr ls)))]))))


  • This recursive function will work (try it!)
  • This is called “poor man’s Y”                     (lambda (length)
  • Now we are going to extract the pattern              (lambda (ls)
                                                           (cond
    in there, so that the same pattern works                [(null? ls) 0]
    for any function.                                       [else (add1 (length (cdr ls)))])))
Three Self-applications
((lambda (length)
   (lambda (ls)
     (cond
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))])))
 (lambda (length)
   (lambda (ls)
     (cond
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))




            Notice that this code has
            three self-aplications, one
            outer and two inner.
Abstract Outer Self-application
((lambda (length)
   (lambda (ls)
     (cond                                           ((lambda (u) (u u))
      [(null? ls) 0]                                  (lambda (length)
      [else (add1 ((length length) (cdr ls)))])))       (lambda (ls)
 (lambda (length)                                         (cond
   (lambda (ls)                                            [(null? ls) 0]
     (cond                                                 [else (add1 ((length length) (cdr ls)))]))))
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))

                                                            •   First, let’s extract the pattern
                                                                which does the outer self-
                                                                application
                                                            •   In compiler terms, this is called
                                                                “common subexpression
                                                                elimination”
Inner Self-application
((lambda (length)
   (lambda (ls)
     (cond                                           ((lambda (u) (u u))
      [(null? ls) 0]                                  (lambda (length)
      [else (add1 ((length length) (cdr ls)))])))       (lambda (ls)
 (lambda (length)                                         (cond
   (lambda (ls)                                            [(null? ls) 0]
     (cond                                                 [else (add1 ((length length) (cdr ls)))]))))
      [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))


                                                          Now we have only one self-
                                                          application left (why not two?)
Abstract Inner Self-application

                                                     ((lambda (u) (u u))
((lambda (u) (u u))                                   (lambda (length)
 (lambda (length)                                       ((lambda (g)
   (lambda (ls)                                            (lambda (ls)
     (cond                                                   (cond
      [(null? ls) 0]                                          [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))            [else (add1 (g (cdr ls)))])))
                                                         (length length))))



 We can now extract the inner
 self-application                                           •    Done in a very similar way
                                                                 as the outer one.
                                                            •    We may call it “factor out”
Function is there!

                                                     ((lambda (u) (u u))
((lambda (u) (u u))                                   (lambda (length)
 (lambda (length)                                       ((lambda (g)
   (lambda (ls)                                            (lambda (ls)
     (cond                                                   (cond
      [(null? ls) 0]                                          [(null? ls) 0]
      [else (add1 ((length length) (cdr ls)))]))))            [else (add1 (g (cdr ls)))])))
                                                         (length length))))




         • Notice that this part is exactly             (define length
                                                         (lambda (ls)
           the definition of “length”                      (cond
           (modulo alpha-equivalence)                       [(null? ls) 0]
                                                            [else (add1 (length (cdr ls)))])))
         • We are almost done!
Non-termination (CBV)
                                 used to be here

    ((lambda (u) (u u))
     (lambda (length)
       ((lambda (g)
          (lambda (ls)
            (cond
             [(null? ls) 0]
             [else (add1 (g (cdr ls)))])))
        (length length))))



•      But notice that (length length)
       went outside of (lambda (ls) …)
•      This will cause non-termination if
       the language is call-by-value
       (why?)
Eta-expansion

((lambda (u) (u u))                        ((lambda (u) (u u))
 (lambda (length)                           (lambda (length)
   ((lambda (g)                               ((lambda (g)
      (lambda (ls)                               (lambda (ls)
        (cond                                      (cond
         [(null? ls) 0]                             [(null? ls) 0]
         [else (add1 (g (cdr ls)))])))              [else (add1 (g (cdr ls)))])))
    (length length))))                         (lambda (v) ((length length) v))))


                                           •   Eta-expand (length length) will
                                               prevent the non-termination while
                                               preserving the semantics
Abstract out the function

                                                    ((lambda (f)
                                                      ((lambda (u) (u u))
((lambda (u) (u u))
                                                       (lambda (length)
 (lambda (length)
                                                         (f
   ((lambda (g)
                                                          (lambda (v) ((length length) v))))))
      (lambda (ls)
        (cond                            “length”
                                                    (lambda (g)
         [(null? ls) 0]
                                                      (lambda (ls)
         [else (add1 (g (cdr ls)))])))
                                                        (cond
    (lambda (v) ((length length) v))))
                                                         [(null? ls) 0]
                                                         [else (add1 (g (cdr ls)))]))))

                                                • Now we can factor out the
                                                  function “length”
                                                • Notice that we can now
                                                  substitute f for any function
                                                  and get a recursive definition!
This is Y combinator!
                                                               Y combinator!

                                         Y combinator
                                                        ((lambda (f)
                                                          ((lambda (u) (u u))
((lambda (u) (u u))
                                                           (lambda (length)
 (lambda (length)
                                                             (f
   ((lambda (g)
                                                              (lambda (v) ((length length) v))))))
      (lambda (ls)
        (cond                                “length”
                                                        (lambda (g)
         [(null? ls) 0]
                                                          (lambda (ls)
         [else (add1 (g (cdr ls)))])))
                                                            (cond
    (lambda (v) ((length length) v))))
                                                             [(null? ls) 0]
                                                             [else (add1 (g (cdr ls)))]))))

                                                    • Now we can factor out the
                                                      function “length”
                                                    • Notice that we can now
                                                      substitute f for any function
                                                      and get a recursive definition!
Renaming


(lambda (f)                                  (lambda (f)
 ((lambda (u) (u u))                           ((lambda (u) (u u))
  (lambda (length)                              (lambda (x)
    (f                                            (f
     (lambda (v) ((length length) v)))))           (lambda (v) ((x x) v))))))




Does the name “length” matter                      • Obviously no!
here?                                              • So we can rename it
Expanding


(lambda (f)                                    (lambda (f)
  ((lambda (u) (u u))                             ((lambda (x) (f (lambda (v) ((x x) v))))
   (lambda (x) (f (lambda (v) ((x x) v))))))       (lambda (x) (f (lambda (v) ((x x) v))))))




                                                   Or, if you would like self-
                                                   application expanded out,
                                                   this is just another form
CBV and CBN

                                  Y combinator (call-by-value)

                                       (lambda (f)
                                          ((lambda (x) (f (lambda (v) ((x x) v))))
                                           (lambda (x) (f (lambda (v) ((x x) v))))))



                                     Y combinator (call-by-name)

Or, if the language is call-by-        (lambda (f)
name, we get this instead                 ((lambda (x) (f (x x))))
(without eta-expansion)                    (lambda (x) (f (x x))))))
Test (length)
(((lambda (f)
    ((lambda (x) (f (lambda (v) ((x x) v))))
     (lambda (x) (f (lambda (v) ((x x) v))))))
  (lambda (length)
    (lambda (ls)
      (cond
       [(null? ls) 0]
       [else (add1 (length (cdr ls)))]))))
 '(a b c))

==> 3
Test (factorial)
(((lambda (f)
    ((lambda (x) (f (lambda (v) ((x x) v))))
     (lambda (x) (f (lambda (v) ((x x) v))))))
  (lambda (fact)
    (lambda (n)
      (cond
       [(zero? n) 1]
       [else (* n (fact (sub1 n)))]))))
 5)

==> 120

More Related Content

What's hot

GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
Kent Chen
 
Effective CMake
Effective CMakeEffective CMake
Effective CMake
Daniel Pfeifer
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machine
Alexei Starovoitov
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
Daniel Pfeifer
 
Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engine
Duoyi Wu
 
Vim Vi Improved
Vim Vi ImprovedVim Vi Improved
Vim Vi Improved
Tusharadri Sarkar
 
Embedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile DevicesEmbedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile Devices
National Cheng Kung University
 
20210506 meeting
20210506 meeting20210506 meeting
20210506 meeting
NickHuang49
 
Introduction to gdb
Introduction to gdbIntroduction to gdb
Introduction to gdb
Owen Hsu
 
Introduction to Celery
Introduction to CeleryIntroduction to Celery
Introduction to Celery
Chathuranga Bandara
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
Ray Jenkins
 
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_finalCSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CanSecWest
 
Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018
Roman Elizarov
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
Basil N G
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesCharles Nutter
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
intelliyole
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Tuning Apache Phoenix/HBase
Tuning Apache Phoenix/HBaseTuning Apache Phoenix/HBase
Tuning Apache Phoenix/HBase
Anil Gupta
 

What's hot (20)

GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
Effective CMake
Effective CMakeEffective CMake
Effective CMake
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machine
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
 
Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engine
 
Vim Vi Improved
Vim Vi ImprovedVim Vi Improved
Vim Vi Improved
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 
Embedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile DevicesEmbedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile Devices
 
20210506 meeting
20210506 meeting20210506 meeting
20210506 meeting
 
Introduction to gdb
Introduction to gdbIntroduction to gdb
Introduction to gdb
 
Introduction to Celery
Introduction to CeleryIntroduction to Celery
Introduction to Celery
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
 
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_finalCSW2017 Qinghao tang+Xinlei ying vmware_escape_final
CSW2017 Qinghao tang+Xinlei ying vmware_escape_final
 
Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018Kotlin Coroutines in Practice @ KotlinConf 2018
Kotlin Coroutines in Practice @ KotlinConf 2018
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
Tuning Apache Phoenix/HBase
Tuning Apache Phoenix/HBaseTuning Apache Phoenix/HBase
Tuning Apache Phoenix/HBase
 

Viewers also liked

Y Combinator Overview
Y Combinator OverviewY Combinator Overview
Y Combinator Overview
Michael Seibel
 
2012 05-08-lambda-draft
2012 05-08-lambda-draft2012 05-08-lambda-draft
2012 05-08-lambda-draftLin Jen-Shin
 
10 most inspirational entrepreneurship quotes by trep talks
10 most inspirational entrepreneurship quotes by trep talks10 most inspirational entrepreneurship quotes by trep talks
10 most inspirational entrepreneurship quotes by trep talks
Sushant Misra
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
Edward Blurock
 
An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
Norman Richards
 
Pop group brochure autopop 2013
Pop group brochure   autopop 2013Pop group brochure   autopop 2013
Pop group brochure autopop 2013PopGroup
 
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov..."Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
Laurence Welford
 
Social Media Changed Events Forever. Here is Proof!
Social Media Changed Events Forever. Here is Proof!Social Media Changed Events Forever. Here is Proof!
Social Media Changed Events Forever. Here is Proof!
Julius Solaris
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
Norman Richards
 
Linux Profiling at Netflix
Linux Profiling at NetflixLinux Profiling at Netflix
Linux Profiling at Netflix
Brendan Gregg
 
Standard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch DeckStandard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch Deck
Zachary Townsend
 
TouristEye - Personalizing The Travel Experience - 500 Startups
TouristEye - Personalizing The Travel Experience - 500 StartupsTouristEye - Personalizing The Travel Experience - 500 Startups
TouristEye - Personalizing The Travel Experience - 500 Startups500 Startups
 
Square pitch deck
Square pitch deckSquare pitch deck
Square pitch deck
pitchenvy
 
task.ly pitch deck
task.ly pitch decktask.ly pitch deck
task.ly pitch deck
Dmitry Gorshkov
 
Kibin
Kibin Kibin
Kibin
500 Startups
 
500’s Demo Day Batch 12 >> Alfred
500’s Demo Day Batch 12 >> Alfred500’s Demo Day Batch 12 >> Alfred
500’s Demo Day Batch 12 >> Alfred
500 Startups
 
BrandBoards demo day pitch deck
BrandBoards demo day pitch deckBrandBoards demo day pitch deck
BrandBoards demo day pitch deck
500 Startups
 

Viewers also liked (20)

Y Combinator Overview
Y Combinator OverviewY Combinator Overview
Y Combinator Overview
 
2012 05-08-lambda-draft
2012 05-08-lambda-draft2012 05-08-lambda-draft
2012 05-08-lambda-draft
 
10 most inspirational entrepreneurship quotes by trep talks
10 most inspirational entrepreneurship quotes by trep talks10 most inspirational entrepreneurship quotes by trep talks
10 most inspirational entrepreneurship quotes by trep talks
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
 
Pop group brochure autopop 2013
Pop group brochure   autopop 2013Pop group brochure   autopop 2013
Pop group brochure autopop 2013
 
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov..."Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
"Inspiring Innovation" HSLU TEDx Event - "Resilience: Key to Successful Innov...
 
Social Media Changed Events Forever. Here is Proof!
Social Media Changed Events Forever. Here is Proof!Social Media Changed Events Forever. Here is Proof!
Social Media Changed Events Forever. Here is Proof!
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
 
Linux Profiling at Netflix
Linux Profiling at NetflixLinux Profiling at Netflix
Linux Profiling at Netflix
 
Standard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch DeckStandard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch Deck
 
TouristEye - Personalizing The Travel Experience - 500 Startups
TouristEye - Personalizing The Travel Experience - 500 StartupsTouristEye - Personalizing The Travel Experience - 500 Startups
TouristEye - Personalizing The Travel Experience - 500 Startups
 
Square pitch deck
Square pitch deckSquare pitch deck
Square pitch deck
 
task.ly pitch deck
task.ly pitch decktask.ly pitch deck
task.ly pitch deck
 
Kibin
Kibin Kibin
Kibin
 
Binpress
BinpressBinpress
Binpress
 
Sverve
SverveSverve
Sverve
 
LaunchRock
LaunchRockLaunchRock
LaunchRock
 
500’s Demo Day Batch 12 >> Alfred
500’s Demo Day Batch 12 >> Alfred500’s Demo Day Batch 12 >> Alfred
500’s Demo Day Batch 12 >> Alfred
 
BrandBoards demo day pitch deck
BrandBoards demo day pitch deckBrandBoards demo day pitch deck
BrandBoards demo day pitch deck
 

Similar to Reinventing the Y combinator

Class 31: Deanonymizing
Class 31: DeanonymizingClass 31: Deanonymizing
Class 31: Deanonymizing
David Evans
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
Mohsen Zainalpour
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
IndicThreads
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
new features in jdk8
new features in jdk8new features in jdk8
new features in jdk8
岩 夏
 
AutoDesk
AutoDeskAutoDesk
AutoDesk
SE3D
 
Scoobi - Scala for Startups
Scoobi - Scala for StartupsScoobi - Scala for Startups
Scoobi - Scala for Startups
bmlever
 
Syntactic Salt and Sugar Presentation
Syntactic Salt and Sugar PresentationSyntactic Salt and Sugar Presentation
Syntactic Salt and Sugar Presentation
grepalex
 
Loom & Functional Graphs in Clojure @ LambdaConf 2015
Loom & Functional Graphs in Clojure @ LambdaConf 2015Loom & Functional Graphs in Clojure @ LambdaConf 2015
Loom & Functional Graphs in Clojure @ LambdaConf 2015
Aysylu Greenberg
 
DevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a DatastoreDevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a Datastore
Xavier Coulon
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
Sami Said
 
Scheme language
Scheme languageScheme language
Scheme language
JITENDRA LENKA
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Sergey Platonov
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language Md. Mahedi Mahfuj
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introductionelliando dias
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Project Lambda - Closures after all?
Project Lambda - Closures after all?Project Lambda - Closures after all?
Project Lambda - Closures after all?
Andreas Enbohm
 

Similar to Reinventing the Y combinator (20)

Clojure intro
Clojure introClojure intro
Clojure intro
 
Class 31: Deanonymizing
Class 31: DeanonymizingClass 31: Deanonymizing
Class 31: Deanonymizing
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
new features in jdk8
new features in jdk8new features in jdk8
new features in jdk8
 
SacalaZa #1
SacalaZa #1SacalaZa #1
SacalaZa #1
 
AutoDesk
AutoDeskAutoDesk
AutoDesk
 
Scoobi - Scala for Startups
Scoobi - Scala for StartupsScoobi - Scala for Startups
Scoobi - Scala for Startups
 
Syntactic Salt and Sugar Presentation
Syntactic Salt and Sugar PresentationSyntactic Salt and Sugar Presentation
Syntactic Salt and Sugar Presentation
 
Loom & Functional Graphs in Clojure @ LambdaConf 2015
Loom & Functional Graphs in Clojure @ LambdaConf 2015Loom & Functional Graphs in Clojure @ LambdaConf 2015
Loom & Functional Graphs in Clojure @ LambdaConf 2015
 
DevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a DatastoreDevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a Datastore
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Scheme language
Scheme languageScheme language
Scheme language
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
 
Java 8
Java 8Java 8
Java 8
 
Project Lambda - Closures after all?
Project Lambda - Closures after all?Project Lambda - Closures after all?
Project Lambda - Closures after all?
 

Recently uploaded

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 

Recently uploaded (20)

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 

Reinventing the Y combinator

  • 1. How to Reinvent the Y combinator Yin Wang
  • 2. Why do we need the Y combinator? For fundamental understanding of recursion, we hope to create recursive functions using the lambda calculus: x | t t | λx.t
  • 3. Why not define or letrec? (define length (letrec ([length (lambda (ls) (lambda (ls) (cond (cond [(null? ls) 0] [(null? ls) 0] [else (add1 (length (cdr ls)))]))) [else (add1 (length (cdr ls)))]))]) (length '(a b c))) 1. We don’t have define or letrec in lambda calculus 2. For fundamental understanding of recursion, we want to see how define and letrec can be created using just the three elements of lambda calculus: x | t t | λx.t 3. You will see how this understanding can be useful when constructing compilers
  • 4. Plan (define length (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))]))) • We start by constructing a recursive definition of “length” in a pure subset of Scheme • Then extract a common pattern that can be applied to recursive definitions in general • This common pattern is the Y combinator
  • 5. How do we do that? • First, notice we can’t really define a recursive function without binding it to a name • Second, answer this question: “Where can we bind something to a name?” • The answer is: λx.t • Lambda, the ultimate binder
  • 6. Step 1: Binder (define length (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))]))) • Step1: create a lambda similar to this define • This creates a binder where we can bind the function to • Our goal: bind the function “length” to the name length
  • 7. Step 1: Binder (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))]))) • Step1: create a lambda similar to this define • This creates a binder where we can bind the function to • Our goal: bind the function “length” to the name length
  • 8. Step 2: Copy ((lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))]))) (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))])))) • Make a copy of the function and apply itself to the copy (self-application) • This will successfully bind the name length to the function “itself”
  • 9. Step 3: Small fix ((lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) The first argument to the application of “length” should be itself
  • 10. Step 4: Extract Patterns ((lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) • This recursive function will work (try it!) • This is called “poor man’s Y” • Now we are going to extract the pattern in there, so that the same pattern works for any function.
  • 11. Step 4: Extract Patterns ((lambda (length) (lambda (ls) (cond • But we can’t see the original [(null? ls) 0] definition in there. [else (add1 ((length length) (cdr ls)))]))) • We hope to see this, but the (lambda (length) self-applications (length length) (lambda (ls) bother us. (cond • Hope we can get rid of them [(null? ls) 0] while preserving the semantics. [else (add1 ((length length) (cdr ls)))])))) • This recursive function will work (try it!) • This is called “poor man’s Y” (lambda (length) • Now we are going to extract the pattern (lambda (ls) (cond in there, so that the same pattern works [(null? ls) 0] for any function. [else (add1 (length (cdr ls)))])))
  • 12. Three Self-applications ((lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) Notice that this code has three self-aplications, one outer and two inner.
  • 13. Abstract Outer Self-application ((lambda (length) (lambda (ls) (cond ((lambda (u) (u u)) [(null? ls) 0] (lambda (length) [else (add1 ((length length) (cdr ls)))]))) (lambda (ls) (lambda (length) (cond (lambda (ls) [(null? ls) 0] (cond [else (add1 ((length length) (cdr ls)))])))) [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) • First, let’s extract the pattern which does the outer self- application • In compiler terms, this is called “common subexpression elimination”
  • 14. Inner Self-application ((lambda (length) (lambda (ls) (cond ((lambda (u) (u u)) [(null? ls) 0] (lambda (length) [else (add1 ((length length) (cdr ls)))]))) (lambda (ls) (lambda (length) (cond (lambda (ls) [(null? ls) 0] (cond [else (add1 ((length length) (cdr ls)))])))) [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) Now we have only one self- application left (why not two?)
  • 15. Abstract Inner Self-application ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (length) ((lambda (g) (lambda (ls) (lambda (ls) (cond (cond [(null? ls) 0] [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) [else (add1 (g (cdr ls)))]))) (length length)))) We can now extract the inner self-application • Done in a very similar way as the outer one. • We may call it “factor out”
  • 16. Function is there! ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (length) ((lambda (g) (lambda (ls) (lambda (ls) (cond (cond [(null? ls) 0] [(null? ls) 0] [else (add1 ((length length) (cdr ls)))])))) [else (add1 (g (cdr ls)))]))) (length length)))) • Notice that this part is exactly (define length (lambda (ls) the definition of “length” (cond (modulo alpha-equivalence) [(null? ls) 0] [else (add1 (length (cdr ls)))]))) • We are almost done!
  • 17. Non-termination (CBV) used to be here ((lambda (u) (u u)) (lambda (length) ((lambda (g) (lambda (ls) (cond [(null? ls) 0] [else (add1 (g (cdr ls)))]))) (length length)))) • But notice that (length length) went outside of (lambda (ls) …) • This will cause non-termination if the language is call-by-value (why?)
  • 18. Eta-expansion ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (length) ((lambda (g) ((lambda (g) (lambda (ls) (lambda (ls) (cond (cond [(null? ls) 0] [(null? ls) 0] [else (add1 (g (cdr ls)))]))) [else (add1 (g (cdr ls)))]))) (length length)))) (lambda (v) ((length length) v)))) • Eta-expand (length length) will prevent the non-termination while preserving the semantics
  • 19. Abstract out the function ((lambda (f) ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (length) (f ((lambda (g) (lambda (v) ((length length) v)))))) (lambda (ls) (cond “length” (lambda (g) [(null? ls) 0] (lambda (ls) [else (add1 (g (cdr ls)))]))) (cond (lambda (v) ((length length) v)))) [(null? ls) 0] [else (add1 (g (cdr ls)))])))) • Now we can factor out the function “length” • Notice that we can now substitute f for any function and get a recursive definition!
  • 20. This is Y combinator! Y combinator! Y combinator ((lambda (f) ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (length) (f ((lambda (g) (lambda (v) ((length length) v)))))) (lambda (ls) (cond “length” (lambda (g) [(null? ls) 0] (lambda (ls) [else (add1 (g (cdr ls)))]))) (cond (lambda (v) ((length length) v)))) [(null? ls) 0] [else (add1 (g (cdr ls)))])))) • Now we can factor out the function “length” • Notice that we can now substitute f for any function and get a recursive definition!
  • 21. Renaming (lambda (f) (lambda (f) ((lambda (u) (u u)) ((lambda (u) (u u)) (lambda (length) (lambda (x) (f (f (lambda (v) ((length length) v))))) (lambda (v) ((x x) v)))))) Does the name “length” matter • Obviously no! here? • So we can rename it
  • 22. Expanding (lambda (f) (lambda (f) ((lambda (u) (u u)) ((lambda (x) (f (lambda (v) ((x x) v)))) (lambda (x) (f (lambda (v) ((x x) v)))))) (lambda (x) (f (lambda (v) ((x x) v)))))) Or, if you would like self- application expanded out, this is just another form
  • 23. CBV and CBN Y combinator (call-by-value) (lambda (f) ((lambda (x) (f (lambda (v) ((x x) v)))) (lambda (x) (f (lambda (v) ((x x) v)))))) Y combinator (call-by-name) Or, if the language is call-by- (lambda (f) name, we get this instead ((lambda (x) (f (x x)))) (without eta-expansion) (lambda (x) (f (x x))))))
  • 24. Test (length) (((lambda (f) ((lambda (x) (f (lambda (v) ((x x) v)))) (lambda (x) (f (lambda (v) ((x x) v)))))) (lambda (length) (lambda (ls) (cond [(null? ls) 0] [else (add1 (length (cdr ls)))])))) '(a b c)) ==> 3
  • 25. Test (factorial) (((lambda (f) ((lambda (x) (f (lambda (v) ((x x) v)))) (lambda (x) (f (lambda (v) ((x x) v)))))) (lambda (fact) (lambda (n) (cond [(zero? n) 1] [else (* n (fact (sub1 n)))])))) 5) ==> 120