Merge pull request #720 from aarroyoc/max-min-list
Add list_max & list_min to lists module
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
memberchk/2, reverse/2, length/2, maplist/2,
|
memberchk/2, reverse/2, length/2, maplist/2,
|
||||||
maplist/3, maplist/4, maplist/5, maplist/6,
|
maplist/3, maplist/4, maplist/5, maplist/6,
|
||||||
maplist/7, maplist/8, maplist/9, same_length/2, nth0/3,
|
maplist/7, maplist/8, maplist/9, same_length/2, nth0/3,
|
||||||
sum_list/2, transpose/2, list_to_set/2]).
|
sum_list/2, transpose/2, list_to_set/2, list_max/2, list_min/2]).
|
||||||
|
|
||||||
|
|
||||||
:- use_module(library(error)).
|
:- use_module(library(error)).
|
||||||
@@ -200,3 +200,16 @@ nth0_search(N, N, [E|_], E).
|
|||||||
nth0_search(N0, N, [_|Es], E) :-
|
nth0_search(N0, N, [_|Es], E) :-
|
||||||
N1 is N0 + 1,
|
N1 is N0 + 1,
|
||||||
nth0_search(N1, N, Es, E).
|
nth0_search(N1, N, Es, E).
|
||||||
|
|
||||||
|
|
||||||
|
list_max([N|Ns], Max) :-
|
||||||
|
foldl(list_max_, Ns, N, Max).
|
||||||
|
|
||||||
|
list_max_(N, Max0, Max) :-
|
||||||
|
Max is max(N, Max0).
|
||||||
|
|
||||||
|
list_min([N|Ns], Min) :-
|
||||||
|
foldl(list_min_, Ns, N, Min).
|
||||||
|
|
||||||
|
list_min_(N, Min0, Min) :-
|
||||||
|
Min is min(N, Min0).
|
||||||
|
|||||||
Reference in New Issue
Block a user