fork download
  1. import Control.Monad
  2.  
  3. import Data.Array
  4.  
  5. yoba rangeArr end start = minimumOp [arr ! (end, x) | x <- [0 .. sizeX]]
  6.  
  7. where bounds' @ ((0, 0), (sizeY, sizeX)) = bounds rangeArr
  8.  
  9. arr = listArray bounds' [value y x | y <- [0 .. sizeY], x <- [0 .. sizeX]]
  10.  
  11. value y _ | y == start = Just 0
  12. value _ 0 = Nothing
  13. value y x = minimumOp [(arr ! (n, x - 1)) `addOp` (rangeArr ! (n, y)) | n <- [0 .. sizeY]]
  14.  
  15. addOp = liftM2 (+)
  16.  
  17. minimumOp = foldl minOp Nothing
  18.  
  19. where Just v1 `minOp` Just v2 = Just $ min v1 v2
  20. Just v1 `minOp` Nothing = Just v1
  21. Nothing `minOp` Just v2 = Just v2
  22. Nothing `minOp` Nothing = Nothing
  23.  
  24.  
  25. main = print $ yoba (toArray2D graph) end start
  26.  
  27. where end = 4
  28. start = 0
  29.  
  30. graph = [[Nothing, Just 7, Just 9, Nothing, Nothing, Just 14],
  31. [Just 7, Nothing, Just 10, Just 15, Nothing, Nothing],
  32. [Just 9, Just 10, Nothing, Just 11, Nothing, Just 2],
  33. [Nothing, Just 15, Just 11, Nothing, Just 6, Nothing],
  34. [Nothing, Nothing, Nothing, Just 6, Nothing, Just 9],
  35. [Just 14, Nothing, Just 2, Nothing, Just 9, Nothing]]
  36.  
  37. toArray2D source = listArray ((0, 0), (size, size)) [source !! y !! x | y <- [0 .. size], x <- [0 .. size]]
  38.  
  39. where size = length source - 1
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )
Linking prog ...
stdout
Just 20