X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.scl.compiler%2Ftests%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Fscl%2FSearch.scl;fp=bundles%2Forg.simantics.scl.compiler%2Ftests%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Fscl%2FSearch.scl;h=352e92c742f0d8840fff5116b70b67a0d26486c6;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/tests/org/simantics/scl/compiler/tests/scl/Search.scl b/bundles/org.simantics.scl.compiler/tests/org/simantics/scl/compiler/tests/scl/Search.scl new file mode 100644 index 000000000..352e92c74 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/tests/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