Reputation: 27
-module(prac).
-export([test_if/1]).
test_if(A) ->
if
A == sir_boss ->
team_leader;
A == worker1 or worker2 or worker3 or worker4 ->
regular;
A == me ->
intern
end.
how do I properly put list or string in an if statement?
Upvotes: 1
Views: 482
Reputation: 20004
It's not clear whether you want to compare strings or return them; I'll assume you're trying to compare them.
Your code is comparing the variable A
to various atoms. If you want to compare strings, you can do it using an if
like this:
-module(prac).
-export([test_if/1]).
test_if(A) ->
if
A == "sir_boss" ->
team_leader;
A == "worker1"; A == "worker2";
A == "worker3"; A == "worker4" ->
regular;
A == "me" ->
intern
end.
The semicolons in the worker comparison clause essentially act as orelse
.
Running this in an Erlang shell shows that it seems to behave as expected:
1> prac:test_if("me").
intern
2> prac:test_if("worker4").
regular
But a better approach than using an if
is to pattern-match arguments in function heads:
test_if("sir_boss") -> team_leader;
test_if("worker1") -> regular;
test_if("worker2") -> regular;
test_if("worker3") -> regular;
test_if("worker4") -> regular;
test_if("me") -> intern.
This is clearer and more idiomatic than using an if
.
Upvotes: 2