What does the “head mismatch” compiler error mean?

假装没事ソ 提交于 2019-12-05 11:06:21

问题


I tries to write code to print Z character.

zzzzzzz
     z
    z
   z
  z
 z
zzzzzzz

But when I compile this code, it throws

D:\erlang\graphics>erlc zeez2.erl
d:/erlang/graphics/zeez2.erl:19: head mismatch
d:/erlang/graphics/zeez2.erl:6: function zeez/3 undefined

I can't fixed this error. I didn't find what wrong in my could.
Does one please suggest me.
Thank you.

-module(zeez2).
-export([main/0]).

main() ->
    L = 8,
    zeez( false ,1, L). % line 6

zeez(true, M,M) ->
    init:stop();

zeez(false, M, N) ->
    io:format("~p~n", [zeez(z, N-M)] ),
    zeez(M rem N =:= 0, M + 1, N );

zeez(true, M, N) ->
    io:format("~p~n", [zeez(space, N-M)] ), % line 16
    zeez(M rem N =:= 0, M + 1 , N );

zeez(space, M) ->
    io:format("~p~n", ["-" ++ zeez(space, M-1)] );

zeez(space, 0) ->
    "Z";

zeez(z, M) ->
    io:format("~p~n", ["Z" ++ zeez(z, M-1)] );

zeez(z,0) ->
    "Z".

回答1:


the problem is that you have mixed up 2 functions:

zeez/2 and zeez/3

If you terminate the zeez/3 function by ending it with a full stop not a semi-colon it should compile:

zeez(true, M, N) ->
    io:format("~p~n", [zeez(space, N-M)] ), % line 16
    zeez(M rem N =:= 0, M + 1 , N );                      <-- should end with .

The error message means, 'hey I'm in zeez/3 and you have thrown in a 2-arity clause, wtf?'




回答2:


You're trying to define two functions, the first with 3 parameters (zeez/3) and another with 2 parameters (zeez/2). The head mismatch error is because the zeez/3 function on the previous line should be terminated with a '.'.

I.e. because you've ended the previous zeez/3 function with a ';', it expects the following declaration to be another match for zeez/3:

zeez(true, M, N) ->
    io:format("~p~n", [zeez(space, N-M)] ), % line 16
    zeez(M rem N =:= 0, M + 1 , N ).

zeez(space, M) ->
    io:format("~p~n", ["-" ++ zeez(space, M-1)] );

You should also note that the compiler will give you warnings about "... previous clause at line xxx always matches" because of the ordering of zees(space, 0) and zeez(space, M). You should put zees(space, 0) before zeez(space, M), because it is more specific.



来源:https://stackoverflow.com/questions/1802680/what-does-the-head-mismatch-compiler-error-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!