Question

I'm trying to call a function (from an external module) in erlang. both beam files are located in the same directory.

 -module(drop2).
 -export([fall_velocity/1]).
 fall_velocity(Distance) -> math:sqrt(2 * 9.8 * Distance).

Then I'm calling

-module(ask).
-export([term/0]).
term() ->
Input = io:read("Enter {x,distance} ? >>"),
Term = element(2,Input),
drop2:fall_velocity(Term).

it gives the following error.I tested individual modules for errors. it is compiling with out any errors or warning.

Eshell V5.10.2  (abort with ^G)
1> ask:term().
Enter {x,distance} ? >>{test,10}.
** exception error: an error occurred when evaluating an arithmetic expression
 in function  drop2:fall_velocity/1 (drop2.erl, line 3)

Not sure why it is throwing arithmetic expression error .

Was it helpful?

Solution

You could read the documentation to figure out that the result is {ok, Term}. You could try the io:read/1 function in the console, then you'd see following:

1> io:read("Enter > ").
Enter > {test, 42}.
{ok,{test,42}}
2>

That means that you need to deconstruct the result of io:read/1 differently, for example like this:

-module(ask).
-export([term/0]).
term() ->
   {ok, {_, Distance}} = io:read("Enter {x, distance} > "),
   drop2:fall_velocity(Distance).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top