[Haskell-cafe] Haskell implementation of longest path algorithm

Henk-Jan van Tuyl hjgtuyl at chello.nl
Sun Feb 22 15:01:05 UTC 2015

On Sun, 22 Feb 2015 10:34:23 +0100, Jeremy <voldermort at hotmail.com> wrote:

> Compared to the Nim code
> [https://github.com/logicchains/LPATHBench/blob/master/nim.nim] for a
> longest path algorithm, Haskell
> [https://github.com/logicchains/LPATHBench/blob/master/hs.hs] looks
> horrendously verbose and ugly, even if you ignore the pragmas and  
> imports.
> Is this idiomatic Haskell style? Could it be clearer, but has to be  
> written
> that way for performance - although it still takes 3.7x as long as the  
> Nim version

You can replace
              case isVisited of
                True -> return ()
                False -> do
              unless isVisited $
in several places.
("unless" is from Control.Monad)

Other options for simplification:

76	        if dist > maxVal then writeIORef max dist else return ())
76	        when (dist > maxVal) $ writeIORef max dist
("when" is also from Control.Monad)

     if dist > maxDist then dist else maxDist
     max dist maxDist
in several places. Note, the you cannot use the function max, if you used  
max as a variable name, like in
   max <- GV.foldM' acc (0::Int32) (nodes V.! (fromIntegral nodeID))
(If you use -Wall when compiling, you probably get a warning when you use  
max as a variable name)

           newMax <- case isVisited of
                          True -> return maxDist
                          False -> do
           newMax <- if isVisited
                       then return maxDist
                       else do

You used fromIntegral quite a lot, could you change some type(s) to  
prevent this?

Henk-Jan van Tuyl

Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.

Haskell programming

More information about the Haskell-Cafe mailing list