top | item 7731169

(no title)

bridger | 11 years ago

When I was doing this exercise with a friend on Understudy, he found solutions to multiplication and exponentiation too. I'm still trying to wrap my head around the exponentiation. Passing a number to another number?!

  (define (multiply number1 number2)
    (lambda (f)
      (number1 (lambda (x) ((number2 f) x)))))
  
  (define (exponentiate base exponent)
    (exponent base))

discuss

order

rspeer|11 years ago

Let's use "twice" as the name of Church numeral 2, and "thrice" as the name of numeral 3.

If you look at the input and output of thrice, it looks like this:

   (function that gets applied once) -> (function that gets applied 3 times)
What if we used the output as the input again?

   (function that gets applied once) -> (function that gets applied 3 times) -> (function that gets applied 9 times)
We have a name for what we just did to the "thrice" function, and that name is "twice". So that's (twice thrice), and we can see it makes something happen 3^2 = 9 times.