为什么这个尾递归函数产生&lt; <loop>&gt;错误?</环>

时间:2014-07-29 16:17:00

标签: haskell tail-recursion

我在下面的函数中遇到了一个奇怪的<<loop>>错误。我将它呈现为原样,因为任何简化尝试都会消除错误。

bellmanFord :: (DistanceMap m) => m -> [Edge] -> Maybe m
bellmanFord m edges = loop m 0 (length edges)
  where
    loop dists step size =
      if step > size then
        Nothing
      else
        let newDists = foldl' proc dists edges
        in
          if identical newDists dists then
            Just dists
          else
            loop newDists (step + 1) size

    proc dists (v1, v2, w) =
      let d = get dists v2
          d' = get dists v1 + Only w
      in
        if d' < d then
          assoc dists v2 d'
        else
          dists

整个计划可用here

1 个答案:

答案 0 :(得分:4)

从你的要点:

instance DistanceMap PotMap where
  get m v = min 0 (get m v)

您对get的定义是无限递归的。