Friday, March 5, 2010

Fizzbuzz

Everybody does it sometime:

-module(fizzbuzz).
-compile(export_all).
go()->
Ciphers = [{5,fizz},{3,buzz}],
lists:map(
fun(I)->
case lists:foldl(
fun({Mod,Sub},Acc)->
case I rem Mod of
0->Acc++atom_to_list(Sub);
_->Acc
end
end,"",Ciphers) of
""->I;
Match->Match
end
end,lists:seq(1,100)).


Honestly, I was horrified at how long it took me - all of fifteen minutes, probably. Some problems just scream 'use a for loop' to such an extent that it's hard to think through a list based way of working. What I do like about this though, is that I didn't stoop to also hardcoding the case of 15 being 'fizzbuzz'. My solution naturally produces the composite, which if this were representative of some bigger system would probably be preferable.

No comments:

Post a Comment