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

Brandon Allbery allbery.b at gmail.com
Wed Jan 4 07:04:29 CET 2012

On Tue, Jan 3, 2012 at 23:21, Siddharth Karandikar <
siddharth.karandikar at 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 at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120104/941ed59a/attachment.htm>

More information about the Beginners mailing list