DEV Community

loading...

Discussion on: FizzBuzz challenge in as many languages as possible

Collapse
oscherler profile image
Olivier “Ölbaum” Scherler

Language: Erlang
Code:

Short version, generate a list (of strings and integers), don’t bother printing:

lists:map(
    fun( X ) ->
        case { X rem 3, X rem 5 } of
            { 0, 0 } -> "FizzBuzz";
            { 0, _ } -> "Fizz";
            { _, 0 } -> "Buzz";
            _ -> X
        end
    end,
    lists:seq( 1, 100 )
).

Full version, print one term per line. Here we have to convert integers to strings:

lists:foreach(
    fun( X ) ->
        io:format(
            "~s~n",
            [
                case { X rem 3, X rem 5 } of
                    { 0, 0 } -> "FizzBuzz";
                    { 0, _ } -> "Fizz";
                    { _, 0 } -> "Buzz";
                    _ -> integer_to_list(X)
                end
            ]
        )
    end,
    lists:seq( 1, 100 )
).