0

我无法在本机模式下运行以下代码。当我尝试时显示错误消息:

警告:此系统未配置为本地代码编译。escript:异常错误:没有函数子句匹配 test_ escript _1383_ 893414 _479613:main([]) (./test, line 5) in function escript:run/2 (escript.erl, line 747) in call from escript:start/ 1 (escript.erl, line 277) init:start_it/1 调用 init:start_em/1

如何配置我的系统以在本机模式下运行?

代码:

#!/usr/bin/env escript

-mode(native). %% to fun faster

main([NStr]) ->
    N = list_to_integer(NStr),
    IJ = [{I, J} || I <- lists:seq(1, N), J <- lists:seq(1, N)],
    lists:foreach(fun({I, J}) -> put_data(I, J, true) end, IJ),
    solve(N, 1, [], 0).

solve(N, J, Board, Count) when N < J ->
    print(N, Board),
    Count + 1;
solve(N, J, Board, Count) ->
    F = fun(I, Cnt) ->
            case get_data(I, J) of
                true  ->
                    put_data(I, J, false),
                    Cnt2 = solve(N, J+1, [I|Board], Cnt),
                    put_data(I, J, true),
                    Cnt2;
                false -> Cnt
            end
        end,
    lists:foldl(F, Count, lists:seq(1, N)).

put_data(I, J, Bool) ->
    put({row, I  }, Bool),
    put({add, I+J}, Bool),
    put({sub, I-J}, Bool).

get_data(I, J) ->
    get({row, I}) andalso get({add, I+J}) andalso get({sub, I-J}).

print(N, Board) ->
    Frame = "+-" ++ string:copies("--", N) ++ "+",
    io:format("~s~n", [Frame]),
    lists:foreach(fun(I) -> print_line(N, I) end, Board),
    io:format("~s~n", [Frame]).

print_line(N, I) ->
    F = fun(X, S) when X == I -> "Q " ++ S;
           (_, S)             -> ". " ++ S
        end,
    Line = lists:foldl(F, "", lists:seq(1, N)),
    io:format("| ~s|~n", [Line]).
4

1 回答 1

6

如果您使用的是 Ubuntu 软件包,请安装erlang-base-hipe而不是erlang-base(一个替换另一个)。HIPE 代表“高性能 Erlang”。

本机编译不是唯一的编译类型。如果-mode(compile)改为使用,脚本将在运行前编译为 BEAM 字节码。无论您的 Erlang 安装是否支持 HIPE,这都有效,并且在大多数情况下都足够快。

您还会收到第二条错误消息:

escript: exception error: no function clause matching test__escript__1383__893414__479613:main([]) (./test, line 5)

这与本机编译无关。这只是意味着您使用零参数调用 escript,但它只接受一个参数。您可以通过在函数中添加第二个子句来使其更加用户友好main

main(Args) ->
    io:format("expected one argument, but got ~b~n", [length(Args)]),
    halt(1).
于 2013-11-11T17:36:05.993 回答