【发布时间】:2021-12-29 20:41:02
【问题描述】:
我已经在这段代码中搞砸了几个小时,但我仍然不知道我做错了什么我有其他功能,这是我唯一遇到问题的功能。
-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 也不是很有帮助
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]).
% |
【问题讨论】:
-
1.在 erlang 中,“小于或等于”拼写为
=<。符号<=用于其他称为二进制理解 的东西。 2.在erlang中,你用句点终止一个表达式,但你的if表达式中间有一个句点。在 erlang 中,您不能只在有效表达式的末尾添加代码——标点符号必然需要更改。 -
我认为我从未见过使用
if语句的erlang 生产代码。首选/最佳实践方法是通过模式匹配和函数头。下面有一些答案展示了使用模式匹配的良好解决方案。
标签: erlang