Erlang语言中约束整数上界求法

1szpjjfi  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(199)

我目前正在做一个项目,我不确定如何在Erlang中得到一个约束整数的上界。
谁能帮帮我?解释给我听。
我真的很感激,我试着在网上找。

ycggw6v2

ycggw6v21#

Recursive definition:

  1. -module(a).
  2. -compile(export_all).
  3. high([X|Xs]) ->
  4. high(Xs, _Result=X).
  5. high([X|Xs], Result) when X > Result ->
  6. high(Xs, X);
  7. high([_|Xs], Result) ->
  8. high(Xs, Result);
  9. high([], Result) ->
  10. Result.

In the shell:

  1. 12> c(a).
  2. a.erl:2: Warning: export_all flag enabled - all functions will be exported
  3. {ok,a}
  4. 13> a:high([3, -11, 2, 11, 4]).
  5. 11

Using a library function:

  1. -module(a).
  2. -compile(export_all).
  3. high(Xs) ->
  4. lists:max(Xs).

In the shell:

  1. 21> c(a).
  2. a.erl:2: Warning: export_all flag enabled - all functions will be exported
  3. {ok,a}
  4. 22> a:high([3, -11, 2, 11, 4]).
  5. 11
展开查看全部

相关问题