From 524c4829f9cc5720c18b8d43bd430d0627edcb89 Mon Sep 17 00:00:00 2001 From: aarne Date: Thu, 17 Nov 2005 23:17:42 +0000 Subject: nondeterministic lexer, e.g. subseqs --- src/GF/Data/Operations.hs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/GF/Data/Operations.hs') diff --git a/src/GF/Data/Operations.hs b/src/GF/Data/Operations.hs index 339a053cf..f5434486f 100644 --- a/src/GF/Data/Operations.hs +++ b/src/GF/Data/Operations.hs @@ -65,14 +65,14 @@ module GF.Data.Operations (-- * misc functions updateAssoc, removeAssoc, -- * chop into separator-separated parts - chunks, readIntArg, + chunks, readIntArg, subSequences, -- * state monad with error; from Agda 6\/11\/2001 STM(..), appSTM, stm, stmr, readSTM, updateSTM, writeSTM, done, -- * error monad class - ErrorMonad(..), checkAgain, checks, allChecks - + ErrorMonad(..), checkAgain, checks, allChecks, doUntil + ) where import Data.Char (isSpace, toUpper, isSpace, isDigit) @@ -656,3 +656,16 @@ allChecks ms = case ms of (m: ms) -> let rs = allChecks ms in handle_ (liftM2 (:) m rs) rs _ -> return [] +doUntil :: ErrorMonad m => (a -> Bool) -> [m a] -> m a +doUntil cond ms = case ms of + a:as -> do + v <- a + if cond v then return v else doUntil cond as + _ -> raise "no result" + +-- subsequences sorted from longest to shortest ; their number is 2^n +subSequences :: [a] -> [[a]] +subSequences = sortBy (\x y -> compare (length y) (length x)) . subs where + subs xs = case xs of + [] -> [[]] + x:xs -> let xss = subs xs in [x:y | y <- xss] ++ xss -- cgit v1.2.3