Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created August 8, 2013 08:52
Show Gist options
  • Save charsyam/6182886 to your computer and use it in GitHub Desktop.
Save charsyam/6182886 to your computer and use it in GitHub Desktop.
get max number in list.
-module(list_max).
-export([list_max/1]).
list_max([Head|Rest]) ->
list_max(Head, Rest).
list_max(Res, []) ->
Res;
list_max(Result_so_far, [Head|Rest]) when Head > Result_so_far ->
list_max(Head, Rest);
list_max(Result_so_far, [Head|Rest]) ->
list_max(Result_so_far, Rest).
-module(list_max2).
-export([list_max/1]).
list_max([Head|Rest]) ->
list_max(Rest, Head).
list_max([], Res) ->
Res;
list_max([Head|Rest], Result_so_far) when Head > Result_so_far ->
list_max(Rest, Head);
list_max([Head|Rest], Result_so_far) ->
list_max(Rest, Result_so_far).
c(list_max).
list_max:list_max([12,1,30,2,3]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment