Erlang _99乘法表
刚学Erlang不久,也是学了点基础,在网上逛了逛看到了篇有意思的面试题
相信学Erlang爱好者也看过吧, 就是尾递归写99乘法表
感觉好奇就写了些:(注释少逻辑简单应该看得懂吧)
1 -module(multiple).
2 -compile(export_all).
3
4 mul(Num) ->
5 case (Num > 0 andalso Num =< 9) of
6 false -> io:format("End! or Not Integer~n");
7 true ->
8 case is_integer(Num) of
9 true ->
10 %%io:format("~p * ~p = ~p~n", [Num, Num+1, Num * (Num+1)]),
11 case calculate(1, Num) of
12 ture -> mul(Num + 1)
13 end;
14 false -> io:format("Not Num~n")
15 end
16 end.
17
18 calculate(Num, Flag) ->
19 if
20 (Num > 0 andalso Num =< Flag) ->
21 io:format("~p * ~p = ~p, ", [Num, Flag, Flag * Num]),
22 calculate(Num+1, Flag);
23 (Num > Flag) ->
24 io:format("~n"),
25 ture
26 end.