[Haskell-beginners] Heathrow to London from lyahgg - infinite loop

Siddharth Karandikar siddharth.karandikar at gmail.com
Wed Jan 4 05:21:26 CET 2012


Hi all,

I am trying out Heathrow to London problem given in Learn You a Haskell book.
There is optimization tip given by author to avoid computing priceA =
sum $ map snd pathA every time. I was trying to implement that like
following -

roadStep :: (Path, Path, Int, Int) -> Section -> (Path, Path, Int, Int)
roadStep (pathA, pathB, cA, cB) (Section a b c) =
         let forwardPriceToA = cA + a
             crossPriceToA = cB + b + c
             forwardPriceToB = cB + b
             crossPriceToB = cA + a + c
             (newPathToA, cA) = if forwardPriceToA <= crossPriceToA
                             then ((A, a):pathA, forwardPriceToA)
                             else ((C, c):(B, b):pathB, crossPriceToA)
             (newPathToB, cB) = if forwardPriceToB <= crossPriceToB
                             then ((B, b):pathB, forwardPriceToB)
                             else ((C, c):(A, a):pathA, crossPriceToB)
         in (newPathToA, newPathToB, cA, cB)

But whenever I run this function -
  roadStep ([], [], 0, 0) (Section 50 10 30)
its going in infinite loop! I am not able to figure out whats causing
this .. any thoughts?


Thanks,
Siddharth



More information about the Beginners mailing list