Haskell platform proposal: split package
Twan van Laarhoven
twanvl at gmail.com
Sat Jul 21 14:59:45 CEST 2012
On 2012-07-21 14:30, Twan van Laarhoven wrote:
> Would the following type work?
>
> data Delimiter a = DelimEltPred (a -> Bool) | DelimSublistPred [a -> Bool]
>
> You can go from the current DelimSublist to DelimSublistPred with just `map
> (==)`. And is the distinction between the DelimEltPred and DelimSublistPred
> then still needed at all?
I attached some code that uses the simplified Delimiter type.
As an aside, the documentation for splitPlaces contains this unhelpful remark:
> The behavior of splitPlaces ls xs when sum ls /= length xs can be inferred
> from the above examples and the fact that splitPlaces is total.
To me that reads like "the documentation of this function is left as an exercise
to the reader". Perhaps say something like:
If the input list is longer than the total of the given lengths, then the
remaining elements are dropped. If the list is shorter than the total of the
given lengths, then the result may contain fewer chunks, and the last chunk may
be shorter.
While `splitPlacesBlanks` could say something like:
If the input list is longer than the total of the given lengths, then the
remaining elements are dropped. If the list is shorter than the total of the
given lengths, then the last several chunk will be shorter or empty.
Twan
-------------- next part --------------
-- | A delimiter is a list of predicates to be matched to a subsequence.
newtype Delimiter a = Delimiter [a -> Bool]
-- | Try to match a delimiter at the start of a list, either failing
-- or decomposing the list into the portion which matched the delimiter
-- and the remainder.
matchDelim :: Delimiter a -> [a] -> Maybe ([a],[a])
matchDelim (Delimiter []) xs = Just ([],xs)
matchDelim (Delimiter _) [] = Nothing
matchDelim (Delimiter (d:ds)) (x:xs)
| d x = fmap (\(h,t) -> (x:h,t)) (matchDelim (Delimiter ds) xs)
| otherwise = Nothing
-- ...
breakDelim :: Delimiter a -> [a] -> ([a],Maybe ([a],[a]))
breakDelim d xxs = case matchDelim d xxs of
Just match -> ([],Just match)
Nothing -> case xxs of
[] -> ([],Nothing)
(x:xs) -> let (ys,match) = breakDelim d xs in (x:ys,match)
-- ...
onSublist :: Eq a => [a] -> Splitter a
onSublist lst = defaultSplitter { delimiter = Delimiter (map (==) lst) }
whenElt :: (a -> Bool) -> Splitter a
whenElt p = defaultSplitter { delimiter = Delimiter [p] }
More information about the Libraries
mailing list