Created
July 26, 2013 16:58
-
-
Save manutter51/6090461 to your computer and use it in GitHub Desktop.
Decided to do one of the exercises from _Programming Elixir_, section 7.5. "Write max(list) that returns the element with the maximum value in the list (This is slightly trickier than it sounds.)" I've got both the max value and the (1-based) index of the max value. My First Elixir Code (aw)!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyList do | |
def mymax([h | t]), do: mymax(h, t) | |
def mymax(v, []), do: v | |
def mymax(v, [h | t]) when h < v, do: mymax(v, t) | |
def mymax(_v, [h | t]), do: mymax(h, t) | |
def mymaxelem([h | t]), do: mymaxelem(0, 1, h, t) | |
def mymaxelem(mx, _ix, _v, []), do: mx + 1 | |
def mymaxelem(mx, ix, v, [h | t]) when v >= h, do: mymaxelem(mx, ix + 1, v, t) | |
def mymaxelem(_mx, ix, _v, [h | t]), do: mymaxelem(ix, ix + 1, h, t) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment