Heathrow to London from lyahgg - infinite loop

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

On Tue, Jan 3, 2012 at 23:21, Siddharth Karandikar < siddharth.karandikar@gmail.com> wrote:
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
Haskell's let is recursive; you bind cA (a second time, which is often not wise) there, but forwardPriceToA also uses cA --- which, because you have introduced a local binding for cA, is going to be that local binding and not the one from the parameters. (I think.) So you get a binding that self-recurses forever. Likewise for cB in the next binding. The solution should be to introduce new names for those bindings (by convention cA' and cB', but you can call them anything as long as they're different from the other names in use). -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Wed, Jan 4, 2012 at 11:34 AM, Brandon Allbery
On Tue, Jan 3, 2012 at 23:21, Siddharth Karandikar
wrote: 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
Haskell's let is recursive; you bind cA (a second time, which is often not wise) there, but forwardPriceToA also uses cA --- which, because you have introduced a local binding for cA, is going to be that local binding and not the one from the parameters. (I think.) So you get a binding that self-recurses forever. Likewise for cB in the next binding.
The solution should be to introduce new names for those bindings (by convention cA' and cB', but you can call them anything as long as they're different from the other names in use).
Yes, you are right. I am accidentally creating a recursive binding. I introduced a new name, and it worked! -Wall showed all the shadowing warnings. Better to keep -Wall always on. :) Thanks!
participants (2)
-
Brandon Allbery
-
Siddharth Karandikar