Erlang - 案例构建
我是 Erlang 新手,并且尝试过一些 Erlang 结构。 我的程序应该表现得像这样:
if x == 42:
print "Hi"
else:
print "Hello"
这是我在 Erlang 中的代码
-module(tested).
-export([main/0]).
main() ->
{ok, X} = io:fread("","~d"),
case X == 42 of
true -> io:fwrite("Hi\n");
false -> io:fwrite("Hello\n")
end.
提前感谢您的帮助。
I'm new to Erlang and I've tried some Erlang constructions.
My program should behave something like that:
if x == 42:
print "Hi"
else:
print "Hello"
Here is my code in Erlang
-module(tested).
-export([main/0]).
main() ->
{ok, X} = io:fread("","~d"),
case X == 42 of
true -> io:fwrite("Hi\n");
false -> io:fwrite("Hello\n")
end.
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
{ok, [X]} = io:fread("","~d")
(X
两边的括号)。fread
返回一个列表作为元组的第二个元素(这在您读取多个标记的情况下有意义),因此您需要先将该元素从列表中取出,然后才能进行比较到42
。请注意,您可以简单地对 X 本身进行模式匹配,而不是对 == 结果进行模式匹配,即:
Use
{ok, [X]} = io:fread("","~d")
(brackets aroundX
).fread
returns a list as the second element of the tuple (which makes sense in case you're reading more than one token), so you need to get the element out of the list before you can compare it to42
.Note that instead of pattern matching on the result of ==, you could simply pattern match on X itself, i.e.: