Proposal #1464: add dropPrefix to Data.List

Henning Thielemann lemming at henning-thielemann.de
Wed Jun 27 02:22:21 EDT 2007


On Wed, 27 Jun 2007, Ian Lynagh wrote:

> I seem to have a copy of this function in everything I write sooner or
> later, so I'd like to propose its addition to Data.List. It strips a
> prefix from a list and, if successful, returns the tail. I often use it
> with pattern guards:
>
>     foo :: String -> IO ()
>     foo x
>      | Just y <- dropPrefix "foo=" = putStrLn ("foo is " ++ show y)
>     foo _ = putStrLn "Unknown"
>
> but you can of course achieve the same by using case etc.
>
> The definition is:
>
>     dropPrefix :: Eq a => [a] -> [a] -> Maybe [a]
>     dropPrefix [] ys = Just ys
>     dropPrefix (x:xs) (y:ys)
>      | x == y = dropPrefix xs ys
>     dropPrefix _ _ = Nothing
>
> Let's try 11 July for a discussion deadline.

Indeed, I have written this function, too, but only one time, namely for
disecting CGI URLs:

maybePrefixOf :: Eq a => [a] -> [a] -> Maybe [a]
maybePrefixOf (x:xs) (y:ys) = if x==y then maybePrefixOf xs ys else Nothing
maybePrefixOf [] ys = Just ys
maybePrefixOf _  [] = Nothing


More information about the Libraries mailing list