
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!