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

**URL:** <https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029>\
**Category:** Ruby\
**Created:** [June 5, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029 "2007-06-05T20:18:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MenTaLguY](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://www.ruby-forum.com/u/MenTaLguY)\
**Post date:** [June 5, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029/1 "2007-06-05T20:18:32Z")

</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:

for n in 1…100  
mult\_3 = ( n % 3 ).zero?  
mult\_5 = ( n % 5 ).zero?  
if mult\_3 or mult\_5  
print “Fizz” if mult\_3  
print “Buzz” if mult\_5  
else  
print n  
end  
puts  
end

-mental

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://www.ruby-forum.com/u/MenTaLguY)\
**Post date:** [June 6, 2007, 3:36am UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029/2 "2007-06-06T03:36:41Z")

</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|  
NULL\_P[l][CONST[CONS[q][ZERO]]][  
LAMBDA2 do |r, t|  
AND[NULL\_P[t]][LESSER\_P[r][n]][CONST[CONS[q][r]]][  
rec[rec][SUCC[q]][t]  
][n]  
end[LENGTH[l]]  
][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|  
LAMBDA do |qr|  
LAMBDA2 do |q, r|  
ZERO\_P[q][ID][FLIP[rec[rec]][q]][CONS[ADD[r][CHAR\_0]][s]]  
end[CAR[qr]][CDR[qr]]  
end[DIVMOD[n][TEN]]  
end }]

FORMAT\_NUM = LAMBDA do |n|  
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|  
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|  
NULL\_P[r][ID][LAMBDA do  
LAMBDA2 do |mult\_3, mult\_5|  
SEQUENCE[  
SEQUENCE[  
OR[mult\_3][mult\_5][  
SEQUENCE[  
mult\_3[LAMBDA { OUTPUT\_STRING[FIZZ] }][ID]  
][  
mult\_5[LAMBDA { OUTPUT\_STRING[BUZZ] }][ID]  
]  
][LAMBDA { OUTPUT\_STRING[FORMAT\_NUM[i]] }]  
][  
LAMBDA { OUTPUT\_STRING[CONS[CHAR\_NEWLINE][NIL\_]] }  
]  
][  
LAMBDA { rec[rec][SUCC[i]][CDR[r]] }  
][nil]  
end[DIVISIBLE\_BY\_P[i][THREE]][DIVISIBLE\_BY\_P[i][FIVE]]  
end][nil]  
end }]

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

FIZZBUZZ[A\_HUNDRED]

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://www.ruby-forum.com/u/MenTaLguY)\
**Post date:** [June 6, 2007, 4:56am UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029/3 "2007-06-06T04:56:55Z")

</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.

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://www.ruby-forum.com/u/MenTaLguY)\
**Post date:** [June 6, 2007, 5:36am UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029/4 "2007-06-06T05:36:57Z")

</div>

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)

That is wild.

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

James Edward G. II

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://www.ruby-forum.com/u/MenTaLguY)\
**Post date:** [June 6, 2007, 5:27am UTC](https://www.ruby-forum.com/t/quiz-fizzbuzz-126-solution-1/106029/5 "2007-06-06T05:27:56Z")

</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
