import Control.Monad import Data.Array yoba :: (Num v, Ord v) => Array (Int, Int) (Maybe v) -> Int -> Int -> Maybe v yoba rangeArr start = \end -> minimumOp [arr ! (end, x) | x <- [0 .. sizeX]] where bounds' @ ((0, 0), (sizeY, sizeX)) = bounds rangeArr arr = listArray bounds' [value y x | y <- [0 .. sizeY], x <- [0 .. sizeX]] value y _ | y == start = Just 0 value _ 0 = Nothing value y x = minimumOp [(arr ! (n, x - 1)) `addOp` (rangeArr ! (n, y)) | n <- [0 .. sizeY]] addOp = liftM2 (+) minimumOp = foldl (\x y -> (min x y) `mplus` x `mplus` y) Nothing main = print $ yoba (toArray2D graph) start end where end = 4 start = 0 rank = 10 ^ 10000 graph = map (map (liftM (* rank))) $ [[Nothing, Just 7, Just 9, Nothing, Nothing, Just 14], [Just 7, Nothing, Just 10, Just 15, Nothing, Nothing], [Just 9, Just 10, Nothing, Just 11, Nothing, Just 2], [Nothing, Just 15, Just 11, Nothing, Just 6, Nothing], [Nothing, Nothing, Nothing, Just 6, Nothing, Just 9], [Just 14, Nothing, Just 2, Nothing, Just 9, Nothing]] toArray2D source = listArray ((0, 0), (size, size)) [source !! y !! x | y <- [0 .. size], x <- [0 .. size]] where size = length source - 1