Created
May 10, 2020 00:08
-
-
Save bruno-cadorette/4d911b95aecd0b4cf4c4c6fe37772067 to your computer and use it in GitHub Desktop.
Revisions
-
bruno-cadorette renamed this gist
May 10, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bruno-cadorette created this gist
May 10, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ import Control.Applicative import qualified Data.Map as Map import Debug.Trace import Data.Foldable import Data.Maybe import Data.Ord import Data.List import Data.List.NonEmpty (nonEmpty) minTransitions :: Ord state => (state -> [transition]) -> (state -> transition -> state) -> (state -> Bool) -> state -> Maybe Int minTransitions findTransitions applyTransition isFinalState initialState = minTrans Map.empty initialState where transitionList currentState cache trans = let newState = applyTransition currentState trans in let result = fmap (+1) (Map.lookup newState cache <|> minTrans cache newState) in (maybe cache (\i -> Map.insert newState i cache) result, result) minTrans cache currentState | isFinalState currentState = Just 0 | otherwise = let trans = findTransitions currentState in let nextSteps = catMaybes $ snd $ mapAccumL (transitionList currentState) cache $ trans in fmap minimum $ nonEmpty $ nextSteps