【问题标题】:syntax error before: 'end' when compiling code之前的语法错误:编译代码时出现“结束”
【发布时间】:2017-01-17 09:32:16
【问题描述】:

我得到了

syntax error before: 'end'

每次我为作业运行此代码时:

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.

有人可以帮我弄清楚为什么会这样吗?谢谢

【问题讨论】:

    标签: erlang


    【解决方案1】:

    min/2 缺少一个 ),而多了一个 end

    应该是:

    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.
    

    【讨论】:

      【解决方案2】:

      你可能想要这样写:

      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.
      

      【讨论】:

        【解决方案3】:

        我觉得这很可疑

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

        应该是

        case length(_PointList) =:= 0 of
        

        此外,您应该获取发现错误的行号(或附近)。错误堆栈跟踪肯定会有所帮助。

        【讨论】:

          猜你喜欢
          • 2015-11-03
          • 1970-01-01
          • 2013-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-23
          • 2020-12-03
          • 1970-01-01
          相关资源
          最近更新 更多