Created
June 23, 2013 14:39
-
-
Save osa1/5845253 to your computer and use it in GitHub Desktop.
dfs
This file contains 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 characters
import Prelude hiding (fail) | |
dfs | |
:: a -- initial state | |
-> path -- empty path | |
-> (a -> [a]) -- branch function to generate states in next level | |
-> (path -> a -> path) -- function to add visited state to path | |
-> (a -> Bool) -- goal checker | |
-> (a -> Bool) -- failure checker, used for backtracking | |
-> [path] -- result is list of paths | |
dfs state emptyPath branch appendPath goal fail = search emptyPath state | |
where | |
search path state | |
| fail state = [] | |
| goal state = [appendPath path state] | |
| otherwise = concatMap (search $ appendPath path state) (branch state) | |
main :: IO () | |
main = do | |
print $ dfs 4 [] (\x -> [x + 3, x + 4, x + 5]) (\p i -> p ++ [i]) (== 14) (> 14) | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment