Last active
August 29, 2015 14:21
-
-
Save ClaireNeveu/c52d7d92ba95a3f9ef10 to your computer and use it in GitHub Desktop.
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 characters
import Prelude hiding (max) | |
data Tree | |
= Alpha Int [Tree] | |
| Beta String [Tree] | |
| Gamma Bool [Tree] | |
children :: Tree -> [Tree] | |
children (Alpha _ ch) = ch | |
children (Beta _ ch) = ch | |
children (Gamma _ ch) = ch | |
data TraverseResult | |
= Continue | |
| Stop | |
| Error String | |
max :: [TraverseResult] -> TraverseResult | |
max (tr : []) = tr | |
max (a : b : rest) = | |
if a `greater` b | |
then max (a : rest) | |
else max (b : rest) | |
where greater (Error _) _ = True | |
greater _ (Error _) = False | |
greater Stop Stop = True | |
greater _ Continue = True | |
greater Continue _ = False | |
type Traversal = Tree -> TraverseResult | |
-- This might need to change. | |
runTraversal :: Traversal -> Tree -> TraverseResult | |
runTraversal f tree = | |
case f tree of | |
Continue -> max $ map f $ children tree | |
res -> res | |
-- What follows definitely needs to change. | |
runTraverser :: Traverser -> Tree -> TraverseResult | |
runTraverser (Traverser ts) tree = | |
max $ map ($ tree) ts | |
data Traverser = Traverser [Traversal] | |
compose :: Traverser -> Traverser -> Traverser | |
compose (Traverser as) (Traverser bs) = Traverser (as ++ bs) | |
-- Usage | |
errorGammaExceptInsideBeta :: Traverser | |
errorGammaExceptInsideBeta = Traverser [f] | |
where f (Alpha _ _) => Continue | |
f (Beta _ _) => Stop | |
f (Gamma _ _) => Error "Found Gamma." | |
errorAlphaExceptInsideGamma :: Traverser | |
errorAlphaExceptInsideGamma = Traverser [f] | |
where f (Alpha _ _) => Error "Found Alpha." | |
f (Beta _ _) => Continue | |
f (Gamma _ _) => Stop | |
t = errorGammaExceptInsideBeta `compose` errorAlphaExceptInsideGamma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment