Skip to content

Instantly share code, notes, and snippets.

@fastjames
Created August 7, 2015 14:13
Show Gist options
  • Select an option

  • Save fastjames/88f6ee2ac8079bfda22e to your computer and use it in GitHub Desktop.

Select an option

Save fastjames/88f6ee2ac8079bfda22e to your computer and use it in GitHub Desktop.

Revisions

  1. fastjames created this gist Aug 7, 2015.
    20 changes: 20 additions & 0 deletions math_mean.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    defmodule FastMath do
    def mean([]), do: 0
    def mean([a]), do: a

    def mean(list) do
    _mean(list, 0, 0)
    end

    # terminal condition
    defp _mean([], total, count) do
    total / count
    end

    defp _mean([head|tail], total, count) do
    new_count = count + 1
    new_total = total + head
    _mean(tail, new_total, new_count)
    end

    end