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;fp=tests%2Forg.simantics.scl.compiler.tests%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Fscl%2FSearch.scl;h=352e92c742f0d8840fff5116b70b67a0d26486c6;hp=0000000000000000000000000000000000000000;hb=0364f8f54b009e9e5de482d5c9d1cb7efb023141;hpb=7ecf07ff9aacab300f1fb900f1f0f97beb1be139 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 new file mode 100644 index 000000000..352e92c74 --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/Search.scl @@ -0,0 +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" +-- +Hello \ No newline at end of file