X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=tests%2Forg.simantics.scl.compiler.tests%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Fscl%2FSearch.scl;h=93947ed90e237e223ac68cd159155b3f9a20ac0c;hp=352e92c742f0d8840fff5116b70b67a0d26486c6;hb=HEAD;hpb=602614f4502aae85ecf3967abb7152d2d62903e3 diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Search.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Search.scl index 352e92c74..93947ed90 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Search.scl +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Search.scl @@ -1,47 +1,47 @@ -import "Prelude" hiding (findFirst) - -infinity = 1e9 - -@inline -findFirst :: (a -> Maybe b) -> [a] -> Maybe b -findFirst f l = loop 0 - where - len = length l - loop i - | i >= len = Nothing - | otherwise = match f (l!i) with - s @ (Just _) -> s - Nothing -> loop (i+1) - -dfsFirst :: (a -> Boolean) -> (a -> [a]) -> [a] -> (Maybe a) -dfsFirst acceptable successors initial = tryAll initial - where - tryAll l = findFirst loop l - loop p - | acceptable p = Just p - | otherwise = tryAll (successors p) - -data Weighted a = Weighted a Double - -//type SearchAlgorithm e a = -// (a -> Boolean) -> (a -> [Weighted a]) -> [Weighted a] -> Weighted (Maybe a) - -//dfs :: SearchAlgorithm e a -dfs :: (a -> Boolean) -> (a -> [Weighted a]) -> [Weighted a] -> Weighted (Maybe a) -dfs acceptable successors initial = foldl loop (Weighted Nothing infinity) initial - where - loop best@(Weighted _ bestW) (Weighted p w) - | w >= bestW = best - | acceptable p = Weighted (Just p) w - | otherwise = foldl loop best - $ map (\(Weighted p' w') -> Weighted p' (w+w')) - $ successors p - -/* -bfs :: SearchAlgorithm e a - -aStar :: (a -> Double) -> SearchAlgorithm e a -*/ -main = "Hello" --- +import "Prelude" hiding (findFirst) + +infinity = 1e9 + +@inline +findFirst :: (a -> Maybe b) -> [a] -> Maybe b +findFirst f l = loop 0 + where + len = length l + loop i + | i >= len = Nothing + | otherwise = match f (l!i) with + s @ (Just _) -> s + Nothing -> loop (i+1) + +dfsFirst :: (a -> Boolean) -> (a -> [a]) -> [a] -> (Maybe a) +dfsFirst acceptable successors initial = tryAll initial + where + tryAll l = findFirst loop l + loop p + | acceptable p = Just p + | otherwise = tryAll (successors p) + +data Weighted a = Weighted a Double + +//type SearchAlgorithm e a = +// (a -> Boolean) -> (a -> [Weighted a]) -> [Weighted a] -> Weighted (Maybe a) + +//dfs :: SearchAlgorithm e a +dfs :: (a -> Boolean) -> (a -> [Weighted a]) -> [Weighted a] -> Weighted (Maybe a) +dfs acceptable successors initial = foldl loop (Weighted Nothing infinity) initial + where + loop best@(Weighted _ bestW) (Weighted p w) + | w >= bestW = best + | acceptable p = Weighted (Just p) w + | otherwise = foldl loop best + $ map (\(Weighted p' w') -> Weighted p' (w+w')) + $ successors p + +/* +bfs :: SearchAlgorithm e a + +aStar :: (a -> Double) -> SearchAlgorithm e a +*/ +main = "Hello" +-- Hello \ No newline at end of file