user3736336
user3736336

Reputation: 137

syntax error before: 'end' when compiling code

I am getting

syntax error before: 'end'

Every single time I run this code for an assignment:

closest(_P, _PointList) -> 
    case (length(_PointList =:= 0)) of 
        true -> {0,0};
        false -> closest(_P, tl(_PointList), distance(_P, hd(_PointList)), 1)
    end.
    % Llength = length(_P),

closest(P, _PointList, _Distance, _Index) -> 
    case (length(_PointList =:= 0)) of 
        true -> {_Index, _Distance};
        false -> 
            New_Distance = min(_Distance, distance(_P, hd(_PointList)),
            case (New_Distance < _Distance) of 
                true -> closest(_P, tl(_PointList), New_Distance, _Index + 1);
                false -> closest(_P, tl(_PointList), _Distance, _Index)
            end
    end
end.

Can someone help me figure out why this is happening? Thanks

Upvotes: 0

Views: 205

Answers (3)

Khanh Hua
Khanh Hua

Reputation: 1116

This looks suspicious to me

case (length(_PointList =:= 0)) of 

Should be

case length(_PointList) =:= 0 of

Besides, you should get the line number at which the error is found (or nearby). An error stacktrace surely helps.

Upvotes: 0

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26131

You probably want write this:

closest(_P, []) -> {0, 0};
closest(P, [H|T]) ->
    closest(P, T, distance(P, H), 0, 1).

closest(_P, [], Distance, ClosestIndex, _Index) ->
    {ClosestIndex, Distance};
closest(P, [H|T], Distance, ClosestIndex, Index) ->
    case distance(P, H) of
        New_Distance when New_Distance < Distance ->
            closest(P, T, New_Distance, Index, Index + 1);
        _ ->
            closest(P, T, Distance, ClosestIndex, Index + 1)
    end.

Upvotes: 0

user3736336
user3736336

Reputation: 137

Was missing a ) for min/2 and had an extra end

Should be:

closest(_P, _PointList, _Distance, _Index) -> 
    case (length(_PointList =:= 0)) of 
        true -> {_Index, _Distance};
        false -> 
            New_Distance = min(_Distance, distance(_P, hd(_PointList))),
            case (New_Distance < _Distance) of 
                true -> closest(_P, tl(_PointList), New_Distance, _Index + 1);
                false -> closest(_P, tl(_PointList), _Distance, _Index)
            end
    end.

Upvotes: 2

Related Questions