【问题标题】:Erlang badarith on IO inputIO输入上的Erlang badarith
【发布时间】:2016-01-11 13:18:29
【问题描述】:

我正在学习 Erlang,我正在尝试使用来自 stdio 输入的一些输入将消息从一个进程传递到另一个进程。

这是我的代码(我知道我可以使用普通函数,但这不是主题)。

-module(play).

-compile(export_all).

calculateArea() ->
  receive
    {rectangle, W, H,SenderPID} -> SenderPID ! {area,W * H};
    {circle, R,SenderPID} -> SenderPID ! {area,3.14 * R * R};
    _ -> io:format("We can only calculate area of rectangles or circles.")
  end,
  calculateArea().

doStuff() ->
  CalculateArea = spawn(play,calculateArea,[]),
  {ok,Width} = io:fread("Enter width","~d"),
  {ok,Height} = io:fread("Enter height","~d"),
  CalculateArea ! {rectangle,Width,Height,self()},
  receive
    {area,Size} -> io:write(Size)
  end,
  io:fwrite("done").

当我运行 play:doStuff(). 时,我收到 {badarith,[{play,calculateArea,0,[{file,"play.erl"},{line,10}]}]} 错误。

我不明白为什么,根据文档,“~d”会给我一个十进制值,如果我打印它肯定看起来像这样。

这里有什么问题?

【问题讨论】:

    标签: erlang


    【解决方案1】:

    io:fread 返回

    Result = {ok, Terms :: [term()]}
           | {error, {fread, FreadError :: io_lib:fread_error()}}
           | server_no_data()
    

    所以WidthHeight 将是每个包含一个数字的列表。使用{ok, [Width/Height]} = ...修复。

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 2010-11-29
      • 1970-01-01
      • 2017-10-03
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多