From 2cc1012a0940586520e746cca566a82772964787 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 25 Nov 2023 23:33:48 -0800 Subject: [PATCH] Add an initial 'lazy list' for doing backtracking Signed-off-by: Danila Fedorin --- src/Bergamot/Search.elm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/Bergamot/Search.elm diff --git a/src/Bergamot/Search.elm b/src/Bergamot/Search.elm new file mode 100644 index 0000000..512aed7 --- /dev/null +++ b/src/Bergamot/Search.elm @@ -0,0 +1,25 @@ +module Bergamot.Search exposing (..) + +type SearchStep a + = Empty + | Found a (Search a) + +type alias Search a = () -> SearchStep a + +map : (a -> b) -> Search a -> Search b +map f s () = + case s () of + Empty -> Empty + Found a sp -> Found (f a) (map f sp) + +interleave : Search a -> Search a -> Search a +interleave s1 s2 () = + case s1 () of + Empty -> s2 () + Found a s1p -> Found a (interleave s2 s1p) + +one : Search a -> Maybe (a, Search a) +one s = + case s () of + Empty -> Nothing + Found a sp -> Just (a, sp)