1

I have a strange <<loop>> error with the function below. I present it as is since any attempt to simplify eliminates the error as well.

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

The whole program is available here.

3
  • 1
    Not a solution to your problem, but why are you counting up to size? Why not count down to 0 from size and use pattern matching to clean up the definition of loop? Something like loop dists 0 = Nothing; loop dists n = let newDists = ... in if ... then Just dists else loop newDists $ n - 1? You'd at least remove the parameter for what step you're on. Commented Jul 29, 2014 at 16:21
  • 1
    what's the error text in full? Commented Jul 29, 2014 at 16:28
  • If removing stuff from your code makes the error disappear, you should take a closer look at the code that you removed right before the error went away. <<loop>> means that you have infinite recursion in one of your functions. Commented Jul 29, 2014 at 16:30

1 Answer 1

4

From your gist:

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

Your definition of get is infinitely recursive.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, thank you very much and sorry for disturbing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.