[Haskell-cafe] Being impure within a 'pure' function

Daniel K. anmeldemails at gmail.com
Wed Apr 22 04:38:23 EDT 2009


Hello,

imagine the following situation: You want to implement e.g. Dijkstra's
algorithm to find a shortest path between nodes u and v in a graph. This
algorithm relies heavily on mutating arrays, so the type signature would
look something like

getDistance :: Graph -> Node -> Node -> IO Int

Not mutating the underlying arrays would probably result in poor
performance. BUT: For a constant graph, the distance between two nodes stays
the same all the time, so in fact getDistance should be a pure function!
So here is my question: Is there a way to write functions in Haskell that do
some IO internally, but that are guaranteed to be side-effect free? Of
course one would have to make sure that the array that is mutated inside
getDistance must not be accessible from outside the function.

Is that possible? If not, wouldn't that be desirable? If not, why not?

Thanks

  Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090422/29c150a2/attachment.htm


More information about the Haskell-Cafe mailing list