From: Ian Lynagh Date: Sat, 14 Jul 2007 23:52:04 +0000 (+0000) Subject: Define stripPrefix; fixes trac #1464 X-Git-Tag: 2007-09-13~48 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=3150dd21ca8b5006c7caeed1eda6b1dad7a6f6db;p=ghc-base.git Define stripPrefix; fixes trac #1464 --- diff --git a/Data/List.hs b/Data/List.hs index ad62961..1c52b50 100644 --- a/Data/List.hs +++ b/Data/List.hs @@ -93,6 +93,8 @@ module Data.List , span -- :: (a -> Bool) -> [a] -> ([a], [a]) , break -- :: (a -> Bool) -> [a] -> ([a], [a]) + , stripPrefix -- :: Eq a => [a] -> [a] -> Maybe [a] + , group -- :: Eq a => [a] -> [[a]] , inits -- :: [a] -> [[a]] @@ -221,6 +223,20 @@ infix 5 \\ -- comment to fool cpp -- ----------------------------------------------------------------------------- -- List functions +-- | The 'stripPrefix' function drops the given prefix from a list. +-- It returns 'Nothing' if the list did not start with the prefix +-- given, or 'Just' the list after the prefix, if it does. +-- +-- > stripPrefix "foo" "foobar" -> Just "bar" +-- > stripPrefix "foo" "foo" -> Just "" +-- > stripPrefix "foo" "barfoo" -> Nothing +-- > stripPrefix "foo" "barfoobaz" -> Nothing +stripPrefix :: Eq a => [a] -> [a] -> Maybe [a] +stripPrefix [] ys = Just ys +stripPrefix (x:xs) (y:ys) + | x == y = stripPrefix xs ys +stripPrefix _ _ = Nothing + -- | The 'elemIndex' function returns the index of the first element -- in the given list which is equal (by '==') to the query element, -- or 'Nothing' if there is no such element.