我目前正在做一个项目,我不确定如何在Erlang中得到一个约束整数的上界。谁能帮帮我?解释给我听。我真的很感激,我试着在网上找。
ycggw6v21#
Recursive definition:
-module(a).-compile(export_all).high([X|Xs]) -> high(Xs, _Result=X).high([X|Xs], Result) when X > Result -> high(Xs, X);high([_|Xs], Result) -> high(Xs, Result);high([], Result) -> Result.
-module(a).
-compile(export_all).
high([X|Xs]) ->
high(Xs, _Result=X).
high([X|Xs], Result) when X > Result ->
high(Xs, X);
high([_|Xs], Result) ->
high(Xs, Result);
high([], Result) ->
Result.
In the shell:
12> c(a). a.erl:2: Warning: export_all flag enabled - all functions will be exported{ok,a}13> a:high([3, -11, 2, 11, 4]). 11
12> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
13> a:high([3, -11, 2, 11, 4]).
11
Using a library function:
-module(a).-compile(export_all).high(Xs) -> lists:max(Xs).
high(Xs) ->
lists:max(Xs).
21> c(a). a.erl:2: Warning: export_all flag enabled - all functions will be exported{ok,a}22> a:high([3, -11, 2, 11, 4]).11
21> c(a).
22> a:high([3, -11, 2, 11, 4]).
1条答案
按热度按时间ycggw6v21#
Recursive definition:
In the shell:
Using a library function:
In the shell: