import Control.Monad import Data.Array yoba rangeArr end start = 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 minOp Nothing where Just v1 `minOp` Just v2 = Just $ min v1 v2 Just v1 `minOp` Nothing = Just v1 Nothing `minOp` Just v2 = Just v2 Nothing `minOp` Nothing = Nothing main = print $ yoba (toArray2D graph) end start where end = 4 start = 0 graph = [[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