# \[SOLUTION\]\[QUIZ\] FizzBuzz (#126) \[solution #1\]

**URL:** <https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127>\
**Category:** ruby-talk\
**Created:** [5 June 2007 18:17 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127 "2007-06-05T18:17:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [5 June 2007 18:17 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/1 "2007-06-05T18:17:55Z")

</div>

I've got two solutions this go-round. First, the solution I would present were I asked to do this in an actual job interview:

&nbsp;&nbsp;for n in 1..100  
&nbsp;&nbsp;&nbsp;&nbsp;mult\_3 = ( n % 3 ).zero?  
&nbsp;&nbsp;&nbsp;&nbsp;mult\_5 = ( n % 5 ).zero?  
&nbsp;&nbsp;&nbsp;&nbsp;if mult\_3 or mult\_5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Fizz" if mult\_3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Buzz" if mult\_5  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print n  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;end

-mental

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [6 June 2007 01:36 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/2 "2007-06-06T01:36:07Z")

</div>

And, here's my second one, written in a subset of Ruby which corresponds  
to a pure (though strict) lambda calculus, building up numbers, strings,  
and everything else entirely from scratch. (Okay, I cheated a little  
bit for actual IO)

Sadly Church numerals are very slow for non-tiny numbers, and Ruby  
doesn't do tail recursion optimization which just makes matters worse.  
But it does work, given enough time and stack space. Try running it and  
see how high you can get!

-mental

alias LAMBDA lambda  
def LAMBDA2(&f) ; LAMBDA { |x| LAMBDA { |y| f[x, y] } } ; end  
def LAMBDA3(&f) ; LAMBDA { |x| LAMBDA { |y| LAMBDA { |z| f[x, y, z] } } } ; end

U = LAMBDA { |f| f[f] }

ID = LAMBDA { |x| x }  
CONST = LAMBDA2 { |y, x| y }  
FLIP = LAMBDA3 { |f,a,b| f[b][a] }  
COMPOSE = LAMBDA3 { |f,g,x| f[g[x]] }

ZERO = CONST[ID]  
SUCC = LAMBDA3 { |n,f,x| f[n[f][x]] }  
ONE = SUCC[ZERO]  
TWO = SUCC[ONE]  
THREE = SUCC[TWO]  
ADD = LAMBDA { |n| n[SUCC] }  
FIVE = ADD[TWO][THREE]  
SIX = ADD[THREE][THREE]  
SEVEN = ADD[FIVE][TWO]  
EIGHT = ADD[FIVE][THREE]  
MULTIPLY = COMPOSE  
FOUR = MULTIPLY[TWO][TWO]  
NINE = MULTIPLY[THREE][THREE]  
TEN = MULTIPLY[FIVE][TWO]  
POWER = LAMBDA2 { |m, n| n[m] }  
A\_HUNDRED = POWER[TEN][TWO]

FALSE\_ = ZERO  
TRUE\_ = CONST  
NOT = FLIP  
OR = LAMBDA2 { |m,n| m[m][n] }  
AND = LAMBDA2 { |m,n| m[n][m] }

ZERO\_P = LAMBDA { |n| n[CONST[FALSE\_]][TRUE\_] }

NIL\_ = LAMBDA { |o| o[nil][TRUE\_] }  
CONS = LAMBDA2 { |h,t| LAMBDA { |o| o[LAMBDA { |g| g[h][t] }][FALSE\_] } }  
NULL\_P = LAMBDA { |p| p[FALSE\_] }  
CAR = LAMBDA { |p| p[TRUE\_][TRUE\_] }  
CDR = LAMBDA { |p| p[TRUE\_][FALSE\_] }  
GUARD\_NULL = LAMBDA3 { |d,f,l| NULL\_P[l][CONST[d]][f][l] }  
FOLDL = U[LAMBDA { |rec| LAMBDA3 { |f,s,l| GUARD\_NULL[s][LAMBDA { |k| rec[rec][f][f[s][CAR[k]]][CDR[k]] }][l] } }]  
DROP = LAMBDA { |n| n[GUARD\_NULL[NIL\_][CDR]] }  
LENGTH = FOLDL[LAMBDA2 { |a, e| SUCC[a] }][ZERO]

MAKE\_LIST = LAMBDA2 { |v,n| n[LAMBDA { |p| CONS[v][p] }][NIL\_] }

LESSER\_P = LAMBDA2 { |m,n| NOT[NULL\_P[DROP[m][MAKE\_LIST[ID][n]]]] }

DIVMOD\_HELPER = U[LAMBDA { |rec| LAMBDA3 do |q,l,n|  
&nbsp;&nbsp;NULL\_P[l][CONST[CONS[q][ZERO]]][  
&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA2 do |r, t|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND[NULL\_P[t]][LESSER\_P[r][n]][CONST[CONS[q][r]]][  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rec[rec][SUCC[q]][t]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][n]  
&nbsp;&nbsp;&nbsp;&nbsp;end[LENGTH[l]]  
&nbsp;&nbsp;][DROP[n][l]]  
end }]  
DIVMOD = LAMBDA2 { |m,n| DIVMOD\_HELPER[ZERO][MAKE\_LIST[ID][m]][n] }

DIVISIBLE\_BY\_P = LAMBDA2 { |m,n| ZERO\_P[CDR[DIVMOD[m][n]]] }

CHAR\_0 = MULTIPLY[SIX][EIGHT]

FORMAT\_NUM\_HELPER = U[LAMBDA { |rec| LAMBDA2 do |s, n|  
&nbsp;&nbsp;LAMBDA do |qr|  
&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA2 do |q, r|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZERO\_P[q][ID][FLIP[rec[rec]][q]][CONS[ADD[r][CHAR\_0]][s]]  
&nbsp;&nbsp;&nbsp;&nbsp;end[CAR[qr]][CDR[qr]]  
&nbsp;&nbsp;end[DIVMOD[n][TEN]]  
end }]

FORMAT\_NUM = LAMBDA do |n|  
&nbsp;&nbsp;ZERO\_P[n][CONST[CONS[CHAR\_0][NIL\_]]][FORMAT\_NUM\_HELPER[NIL\_]][n]  
end

CHAR\_F = MULTIPLY[SEVEN][TEN]  
CHAR\_i = ADD[A\_HUNDRED][FIVE]  
CHAR\_z = ADD[A\_HUNDRED][ADD[MULTIPLY[TWO][TEN]][TWO]]  
CHAR\_B = MULTIPLY[SIX][ADD[TEN][ONE]]  
CHAR\_u = ADD[A\_HUNDRED][ADD[TEN][SEVEN]]

CHAR\_NEWLINE = TEN

FIZZ = CONS[CHAR\_F][CONS[CHAR\_i][CONS[CHAR\_z][CONS[CHAR\_z][NIL\_]]]]  
BUZZ = CONS[CHAR\_B][CONS[CHAR\_u][CONS[CHAR\_z][CONS[CHAR\_z][NIL\_]]]]

OUTPUT\_STRING = LAMBDA do |s|  
&nbsp;&nbsp;print FOLDL[LAMBDA2 { |a,e| a \<\< e }][[]][s].map { |i| i[LAMBDA { |s| s + 1 }][0] }.pack("C\*")  
end

SEQUENCE = FLIP[COMPOSE]

FIZZBUZZ\_HELPER = U[LAMBDA { |rec| LAMBDA2 do |i,r|  
&nbsp;&nbsp;NULL\_P[r][ID][LAMBDA do  
&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA2 do |mult\_3, mult\_5|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SEQUENCE[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SEQUENCE[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OR[mult\_3][mult\_5][  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SEQUENCE[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mult\_3[LAMBDA { OUTPUT\_STRING[FIZZ] }][ID]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mult\_5[LAMBDA { OUTPUT\_STRING[BUZZ] }][ID]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][LAMBDA { OUTPUT\_STRING[FORMAT\_NUM[i]] }]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA { OUTPUT\_STRING[CONS[CHAR\_NEWLINE][NIL\_]] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA { rec[rec][SUCC[i]][CDR[r]] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;][nil]  
&nbsp;&nbsp;&nbsp;&nbsp;end[DIVISIBLE\_BY\_P[i][THREE]][DIVISIBLE\_BY\_P[i][FIVE]]  
&nbsp;&nbsp;end][nil]  
end }]

FIZZBUZZ = LAMBDA do |c|  
&nbsp;&nbsp;FIZZBUZZ\_HELPER[ONE][MAKE\_LIST[ID][c]]  
end

FIZZBUZZ[A\_HUNDRED]

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [6 June 2007 02:56 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/3 "2007-06-06T02:56:42Z")

</div>

MenTaLguY wrote:

> And, here's my second one, written in a subset of Ruby which corresponds  
> to a pure (though strict) lambda calculus, building up numbers, strings,  
> and everything else entirely from scratch. (Okay, I cheated a little  
> bit for actual IO)
> 
> Sadly Church numerals are very slow for non-tiny numbers, and Ruby  
> doesn't do tail recursion optimization which just makes matters worse.  
> But it does work, given enough time and stack space. Try running it and  
> see how high you can get!

Ok, you're hired. Your first project is to write a web server in Malbolge.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [6 June 2007 03:27 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/4 "2007-06-06T03:27:24Z")

</div>

Incidentally, this second solution could probably be made to run in a  
reasonable time if the mod-15 pattern in the output were exploited. But  
I'm lambda'd out at the moment, so it will remain an exercise for the  
reader. 🙂

-mental

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [6 June 2007 03:35 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/5 "2007-06-06T03:35:24Z")

</div>

That is wild.

I'm just staring at the code. I know enlightenment is hidden in there somewhere...

James Edward Gray II

> **···**
>
> On Jun 5, 2007, at 8:36 PM, MenTaLguY wrote:
> 
> > And, here's my second one, written in a subset of Ruby which corresponds to a pure (though strict) lambda calculus, building up numbers, strings, and everything else entirely from scratch. (Okay, I cheated a little bit for actual IO)

---

<div class="post-metadata">

**Author:** ![Brad\_Phelan1](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Brad\_Phelan1](https://rubytalk.org/u/Brad_Phelan1)\
**Post date:** [6 June 2007 17:25 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/6 "2007-06-06T17:25:23Z")

</div>

MenTaLguY wrote:

> And, here's my second one, written in a subset of Ruby which corresponds  
> to a pure (though strict) lambda calculus, building up numbers, strings,  
> and everything else entirely from scratch. (Okay, I cheated a little  
> bit for actual IO)
> 
> Sadly Church numerals are very slow for non-tiny numbers, and Ruby  
> doesn't do tail recursion optimization which just makes matters worse.  
> But it does work, given enough time and stack space. Try running it and  
> see how high you can get!

I thought it was randomly generated joke code jibberish till I copied  
and ran it and it worked .. slowly. So I started revising my lambda  
calculus till my brain hurt... So I ask a question...

Using your Ruby Church numerals is it possible to test for equality? Perhapps you are doing it but I was unable to parse the  
whole algorithm out and figure out exactly what all the lambdas  
you use do.

Cheers

Brad

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [7 June 2007 02:25 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/7 "2007-06-07T02:25:39Z")

</div>

Sure!

EQUAL\_P = LAMBDA2 { |m,n| NOT[OR[LESSER\_P[m][n]][LESSER\_P[n][m]]] }

-mental

> **···**
>
> On Thu, 2007-06-07 at 02:25 +0900, Brad Phelan wrote:
> 
> > Using your Ruby Church numerals is it possible to test for equality?

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [7 June 2007 04:34 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/8 "2007-06-07T04:34:20Z")

</div>

If it helps, what a lot of the math stuff does is create lists of the  
length specified by a number, manipulate those, and then count their  
lengths to get back to a number. For instance, subtraction would be:

SUBTRACT = LAMBDA2 { |m,n| LENGTH[DROP[n][MAKE\_LIST[ID][m]]] }

(here, ID is just used as a dummy value to populate the list with)

-mental

> **···**
>
> On Thu, 2007-06-07 at 02:25 +0900, Brad Phelan wrote:
> 
> > Perhapps you are doing it but I was unable to parse the  
> > whole algorithm out and figure out exactly what all the lambdas  
> > you use do.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [7 June 2007 09:04 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/9 "2007-06-07T09:04:02Z")

</div>

> \> Perhapps you are doing it but I was unable to parse the  
> \> whole algorithm out and figure out exactly what all the lambdas  
> \> you use do.
> 
> If it helps, what a lot of the math stuff does is create lists of the  
> length specified by a number, manipulate those, and then count their  
> lengths to get back to a number. For instance, subtraction would be:
> 
> SUBTRACT = LAMBDA2 { |m,n| LENGTH[DROP[n][MAKE\_LIST[ID][m]]] }

must work nicely for n\>m 😉  
But interesting stuff.

Cheers  
Robert

> **···**
>
> On 6/7/07, MenTaLguY \<mental@rydia.net\> wrote:
> 
> > On Thu, 2007-06-07 at 02:25 +0900, Brad Phelan wrote:
> > 
> > (here, ID is just used as a dummy value to populate the list with)
> > 
> > -mental
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Brad\_Phelan](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@Brad\_Phelan](https://rubytalk.org/u/Brad_Phelan)\
**Post date:** [7 June 2007 11:45 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/10 "2007-06-07T11:45:04Z")

</div>

MenTaLguY wrote:

> > Perhapps you are doing it but I was unable to parse the  
> > whole algorithm out and figure out exactly what all the lambdas  
> > you use do.
> 
> If it helps, what a lot of the math stuff does is create lists of the  
> length specified by a number, manipulate those, and then count their  
> lengths to get back to a number. For instance, subtraction would be:
> 
> SUBTRACT = LAMBDA2 { |m,n| LENGTH[DROP[n][MAKE\_LIST[ID][m]]] }
> 
> (here, ID is just used as a dummy value to populate the list with)
> 
> -mental

Thanks for the pointers. I don't have too much time today to think about this ( public holiday and I'm going out in the sun ) but I know when  
I do I'll want to know the concept here behind lists. Are they real  
lists as in Ruby Array which I doubt or abstract concepts like  
the Church Numerals.

I'll take a look Monday and see if I can understand more.

Regards

Brad

> **···**
>
> > On Thu, 2007-06-07 at 02:25 +0900, Brad Phelan wrote:

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [7 June 2007 16:47 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/11 "2007-06-07T16:47:14Z")

</div>

Church numerals correspond to the natural numbers, so negative results aren't possible. For n\>m, I could either let the computation diverge or return ZERO -- I opted to return ZERO.

It is possible to build a representation of general integers atop Church numerals (one obvious way would be to use a pair consisting of a sign flag and a magnitude), but that was more than I needed for this particular problem.

-mental

> **···**
>
> On Thu, 7 Jun 2007 18:04:02 +0900, "Robert Dober" \<robert.dober@gmail.com\> wrote:
> 
> > > SUBTRACT = LAMBDA2 { |m,n| LENGTH[DROP[n][MAKE\_LIST[ID][m]]] }
> 
> > must work nicely for n\>m 😉

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [7 June 2007 17:12 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/12 "2007-06-07T17:12:26Z")

</div>

Lists are linked lists made of pairs, as in Lisp. Since we chose to represent true and false as two-argument functions which return their first or second argument, respectively, we can take advantage of this to describe a pair as a function which takes true or false as an argument to select an element of the pair.

Such a pair can be created by a function like:

MAKE\_PAIR = LAMBDA2 { |first, second| LAMBDA { |which| which[first][second] } }

And functions for extracting the first or second value from a pair constructed by MAKE\_PAIR can be written like this:

FIRST = LAMBDA { |pair| pair[TRUE\_] }  
SECOND = LAMBDA { |pair| pair[FALSE\_] }

My definitions for CONS, CAR, and CDR in fizzbuzz were a little more involved, because I also needed to be able to represent an empty list (and test for it). So, what I did is roughly equivalent to:

NIL\_ = MAKE\_PAIR[nil][TRUE\_]  
CONS = LAMBDA2 { |head, tail| MAKE\_PAIR[MAKE\_PAIR[head, tail]][FALSE\_] }  
CAR = LAMBDA { |cell| FIRST[FIRST[cell]] }  
CDR = LAMBDA { |cell| SECOND[FIRST[cell]] }  
NULL\_P = LAMBDA { |cell| SECOND[cell] }

A tagged data structure, basically, with a flag indicating whether a cell is a null list or not.

-mental

> **···**
>
> On Thu, 7 Jun 2007 20:45:04 +0900, Brad Phelan \<bradphelan@xtargets.com\> wrote:
> 
> > Thanks for the pointers. I don't have too much time today to think about  
> > this ( public holiday and I'm going out in the sun ) but I know when  
> > I do I'll want to know the concept here behind lists.

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [7 June 2007 17:36 UTC](https://rubytalk.org/t/solution-quiz-fizzbuzz-126-solution-1/38127/13 "2007-06-07T17:36:37Z")

</div>

In some sense, these things are only as abstract as you want them to be. Instead of implementing Church numerals like this (expanded a bit for clarity):

ZERO = LAMBDA2 { |f,x| x }  
ONE = LAMBDA2 { |f,x| f }  
SUCC = LAMBDA { |n| LAMBDA2 { |f,x| f[n[f]] } }  
ADD = LAMBDA2 { |m,n| m[SUCC][n] }  
MULTIPLY = LAMBDA2 { |m,n| LAMBDA2 { |f,x| m[n[f]] } }  
POWER = LAMBDA2 { |m,n| n[m] }

I could also have done something like this:

class ChurchNumeral  
&nbsp;&nbsp;&nbsp;attr\_reader :value

&nbsp;&nbsp;&nbsp;def initialize(value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@value = value  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def call(f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA { |x| @value.times { x = f } ; x }  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;alias call  
end

ZERO = ChurchNumeral.new 0  
ONE = ChurchNumeral.new 1  
SUCC = LAMBDA do |n|  
&nbsp;&nbsp;&nbsp;if ChurchNumeral === n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ChurchNumeral.new n.value + 1  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA2 { |f,x| f[n[f]] }  
&nbsp;&nbsp;&nbsp;end  
end  
ADD = LAMBDA2 do |m,n|  
&nbsp;&nbsp;&nbsp;if ChurchNumeral === m and ChurchNumeral === n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ChurchNumeral.new m.value + n.value  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[SUCC][n]  
&nbsp;&nbsp;&nbsp;end  
end  
MULTIPLY = LAMBDA2 do |m,n|  
&nbsp;&nbsp;&nbsp;if ChurchNumeral === m and ChurchNumeral === n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ChurchNumeral.new m.value \* n.value  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LAMBDA2 { |f,x| m[n[f]] }  
&nbsp;&nbsp;&nbsp;end  
end  
POWER = LAMBDA2 do |m,n|  
&nbsp;&nbsp;&nbsp;if ChurchNumeral === m and ChurchNumeral === n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ChurchNumeral.new m.value \*\* n.value  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n[m]  
&nbsp;&nbsp;&nbsp;end  
end

As described in my previous email, I did take the former sort of approach for lists though.

-mental

> **···**
>
> On Thu, 7 Jun 2007 20:45:04 +0900, Brad Phelan \<bradphelan@xtargets.com\> wrote:
> 
> > Are they real lists as in Ruby Array which I doubt or abstract concepts like  
> > the Church Numerals.
