Reputation: 21
I been messing around with this piece of code for a couple hours and I still don't know what I'm doing wrong i have other functions and this is the only one i been having trouble with.
-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: The shell hasn't been really helpful either
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]).
% |
Upvotes: 2
Views: 85
Reputation: 48599
The function clauses (or if conditions) can simply look like this:
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)] ).
Similar to an if expression, erlang examines the first function clause and if there's a match, that function clause is executed, and no subsequent functions clauses are examined after that. If the clause doesn't match, then the next function clause is examined, etc.
output:
~/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>
Also, very different in erlang(although not relevant here) is that the parameters of a function clause can be constants:
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.
In most languages, all the parameters in a function definition have to be variables.
In erlang, function clause matching is an extremely powerful way to dissect parameters as well as control the flow of execution.
Upvotes: 1
Reputation: 1958
@alias answer is the right one, although I'm pretty sure that the final true
clause will never match since regardless of the value of TEMP
, it will always fall into one of the previous clauses.
OTOH, you might want to consider avoiding if
statements altogether and just using a multi-claused function with guards…
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]).
Upvotes: 3
Reputation: 30428
Your syntax is a bit off. Try the following:
-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.
Upvotes: 1