【问题标题】:Recursive function and stopping condition in HaskellHaskell中的递归函数和停止条件
【发布时间】:2013-03-31 20:29:31
【问题描述】:

我正在实现一个递归函数,我希望停止条件是 (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 "*") 这是否意味着我不能在参数上使用某些函数?

提前致谢

【问题讨论】:

标签: haskell recursion


【解决方案1】:

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

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

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

【讨论】:

  • 我忘了这个!你的建议很有用
猜你喜欢
  • 2015-08-30
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多