1

我正在实现一个递归函数,我希望停止条件是(2*scope),它是函数的参数

sortByManhattanDistance agent (2*scope) scope xs sortedNearFoodList = sortedNearFoodList

sortByManhattanDistance agent n scope xs sortedNearFoodList = sortByManhattanDistance agent (n+1) scope xs (sorted ++ sortedNearFoodList) 
where sorted=compareManhattanDistance xs agent n

和拥抱抱怨:Syntax error in declaration (unexpected symbol "*") 这是否意味着我不能在参数上使用某些功能?

提前致谢

4

1 回答 1

5

不,您不能在这样的等式左侧使用函数或运算符。

做你想做的事情的正确方法是使用警卫

sortByManhattanDistance agent n scope xs sortedNearFoodList
    | n == 2 * scope = sortedNearFoodList
    | otherwise      = sortByManhattanDistance agent (n+1) scope xs
                                            (sorted ++ sortedNearFoodList) 
  where sorted = compareManhattanDistance xs agent n
于 2013-03-31T12:29:31.000 回答