erlang 整数和原子之间的IF语句

goucqfw6  于 2022-12-08  发布在  Erlang
关注(0)|答案(3)|浏览(111)

我一直在摆弄这段代码几个小时,我仍然不知道我做错了什么,我有其他的功能,这是唯一一个我一直有麻烦。

-module(if_statements).

-export([weather/1]).

weather(TEMP) ->

if 

    TEMP <= 0 ->

        io:fwrite("freezing\\n");

    0 < TEMP <= 10 ->

        io:fwrite("cold\\n");

    10 < TEMP <= 20 ->

        io:fwrite("normal\\n");

    20 < TEMP <= 30 -> 

        io:fwrite("hot\\n");

    30 < TEMP <= 40 -> 

        io:fwrite("heated\\n");

    40 < TEMP ->

        io:fwrite("hirviendo\\n").

    true -> 

        io:fwrite("unknown")

end.

Shell:Shell也没有什么帮助

if_statements.erl:6:8: syntax error before: '<='

% 6| TEMP <= 0 ->

% | ^


if_statements.erl:18:8: syntax error before: '->'

% 18| true ->

% | ^


if_statements.erl:2:2: function clima/1 undefined

% 2| -export([weather/1]).

% |
q5lcpyga

q5lcpyga1#

@alias的答案是正确的,尽管我很确定最后的true子句永远不会匹配,因为不管TEMP的值是什么,它总是落入前面的一个子句中。OTOH,你可能需要考虑完全避免if语句,而只使用带保护的多子句函数...

weather(TEMP) when            TEMP =< 0  -> io:fwrite("freezing\n");
weather(TEMP) when  0 < TEMP, TEMP =< 10 -> io:fwrite("cold\n");
weather(TEMP) when 10 < TEMP, TEMP =< 20 -> io:fwrite("normal\n");
weather(TEMP) when 20 < TEMP, TEMP =< 30 -> io:fwrite("hot\n");
weather(TEMP) when 30 < TEMP, TEMP =< 40 -> io:fwrite("heated\n");
weather(TEMP) when 40 < TEMP             -> io:fwrite("boiling ;)\n");
weather(TEMP) -> io:format("unknown temperature: ~p", [TEMP]).
oknrviil

oknrviil2#

您的语法有点不对。请尝试以下操作:

-module(if_statements).
-export([weather/1]).

weather(TEMP) ->
  if
      TEMP =< 0            -> io:fwrite("freezing\n");
      0 < TEMP, TEMP =< 10 -> io:fwrite("cold\n");
     10 < TEMP, TEMP =< 20 -> io:fwrite("normal\n");
     20 < TEMP, TEMP =< 30 -> io:fwrite("hot\n");
     30 < TEMP, TEMP =< 40 -> io:fwrite("heated\n");
     40 < TEMP             -> io:fwrite("hirviendo\n");
     true                  -> io:fwrite("unknown")
end.
eit6fx6z

eit6fx6z3#

函数子句(或if条件)可以简单地如下所示:

weather2(TEMP) when TEMP =< 0  -> "freezing";
weather2(TEMP) when TEMP =< 10 -> "cold";
weather2(TEMP) when TEMP =< 20 -> "normal";
weather2(TEMP) when TEMP =< 30 -> "hot";
weather2(TEMP) when TEMP =< 40 -> "heated";
weather2(_) -> "unknown".  %% this clause only matches if TEMP is greater than 40

test() ->
    print_weather(-10),
    print_weather(7),
    print_weather(20),
    print_weather(21),
    print_weather(35),
    print_weather(45),
    test_finished.

print_weather(TEMP) ->
    io:format("~3w => ~s~n", [TEMP, weather2(TEMP)] ).

与if表达式类似,erlang检查第一个函数子句,如果匹配,则执行该函数子句,之后不再检查后续函数子句。如果子句不匹配,则检查下一个函数子句,依此类推。
输出:

~/erlang_programs$ erl
Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V12.0.2  (abort with ^G)

1> c(a).
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^

{ok,a}

2> a:test().
-10 => freezing
  7 => cold
 20 => normal
 21 => hot
 35 => heated
 45 => unknown
test_finished

3>

另外,在Erlang中非常不同的是(尽管这里不相关),函数子句的参数可以是常量:

f1(X, 10) -> ... ;  %integer
f1(X, a)  -> ....;  %atom
f1(X, "hello") -> ...; %string (which is a shortcut for creating a list)
f1(3, [Name, Age, Phone]) -> something.

在大多数语言中,函数定义中的所有参数都必须是变量。
在Erlang中,function clause matching 是剖析参数和控制执行流的一种非常强大的方法。

相关问题