<div dir="ltr"><p>I've got this fizzbuzz project I am using for a blog series, among
other things. In this version, the fizzbuzz function is fed from a
Fibonacci generator. I'm particularly concerned with the efficiency of
the Fibonacci generator, but all scrutiny is welcomed.</p>
<p>I'll included a link to the entire project, but below are the parts I
think would be sufficient to spot trouble with how I am generating
Fibonacci numbers.</p>
<pre style="" class=""><code><span class="">-- Driver function performs following</span><span class="">
</span><span class="">-- (1) checks that input is proper</span><span class="">
</span><span class="">-- (2) creates integer list for fibonacci generator</span><span class="">
</span><span class="">-- (3) calculates first x in fibonnaci sequence</span><span class="">
</span><span class="">-- (4) generates fizzbuzz output using (3)</span><span class="">
fizzBuzzFib </span><span class="">::</span><span class=""> </span><span class="">[</span><span class="">Text</span><span class="">]</span><span class=""> </span><span class="">-></span><span class=""> Either FizzError </span><span class="">[</span><span class="">Text</span><span class="">]</span><span class="">
fizzBuzzFib str </span><span class="">=</span><span class="">
mapM fizzbuzz </span><span class="">=<<</span><span class="">
mapM fibb </span><span class="">=<<</span><span class=""> </span><span class="">-- Possible problem here</span><span class="">
</span><span class="">(\</span><span class="">x </span><span class="">-></span><span class=""> Right </span><span class="">[</span><span class="">1</span><span class=""> </span><span class="">..</span><span class=""> x</span><span class="">])</span><span class=""> </span><span class="">=<<</span><span class="">
convertToPInt </span><span class="">=<<</span><span class="">
mustHaveOne str
fibb </span><span class="">::</span><span class=""> Integer </span><span class="">-></span><span class=""> Either FizzError Integer
fibb n </span><span class="">=</span><span class=""> Right </span><span class="">$</span><span class=""> snd </span><span class="">.</span><span class=""> foldl' fib' </span><span class="">(</span><span class="">1</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">)</span><span class=""> </span><span class="">.</span><span class=""> map </span><span class="">(</span><span class="">toEnum </span><span class="">.</span><span class=""> fromIntegral</span><span class="">)</span><span class=""> </span><span class="">$</span><span class=""> unfoldl divs n
</span><span class="">where</span><span class="">
unfoldl f x </span><span class="">=</span><span class="">
</span><span class="">case</span><span class=""> f x </span><span class="">of</span><span class="">
Nothing </span><span class="">-></span><span class=""> </span><span class="">[]</span><span class="">
Just </span><span class="">(</span><span class="">u</span><span class="">,</span><span class=""> v</span><span class="">)</span><span class=""> </span><span class="">-></span><span class=""> unfoldl f v </span><span class="">++</span><span class=""> </span><span class="">[</span><span class="">u</span><span class="">]</span><span class="">
divs </span><span class="">0</span><span class=""> </span><span class="">=</span><span class=""> Nothing
divs k </span><span class="">=</span><span class=""> Just </span><span class="">(</span><span class="">uncurry </span><span class="">(</span><span class="">flip </span><span class="">(,))</span><span class=""> </span><span class="">(</span><span class="">k </span><span class="">`</span><span class="">divMod</span><span class="">`</span><span class=""> </span><span class="">2</span><span class="">))</span><span class="">
fib' </span><span class="">(</span><span class="">f</span><span class="">,</span><span class=""> g</span><span class="">)</span><span class=""> p
</span><span class="">|</span><span class=""> p </span><span class="">=</span><span class=""> </span><span class="">(</span><span class="">f</span><span class="">*(</span><span class="">f</span><span class="">+</span><span class="">c</span><span class="">*</span><span class="">g</span><span class="">),</span><span class=""> f</span><span class="">^</span><span class="">c </span><span class="">+</span><span class=""> g</span><span class="">^</span><span class="">c</span><span class="">)</span><span class="">
</span><span class="">|</span><span class=""> otherwise </span><span class="">=</span><span class=""> </span><span class="">(</span><span class="">f</span><span class="">^</span><span class="">c</span><span class="">+</span><span class="">g</span><span class="">^</span><span class="">c</span><span class="">,</span><span class=""> g</span><span class="">*(</span><span class="">c</span><span class="">*</span><span class="">f</span><span class="">-</span><span class="">g</span><span class="">))</span><span class="">
</span><span class="">where</span><span class="">
c </span><span class="">::</span><span class=""> Integer </span><span class="">-- See codebase for reasons</span><span class="">
c </span><span class="">=</span><span class=""> </span><span class="">2<br><br></span></code>The whole project, for your critiquing eye:
<a href="https://github.com/mlitchard/swiftfizz" rel="nofollow">https://github.com/mlitchard/swiftfizz</a><br></pre></div>