From 3150dd21ca8b5006c7caeed1eda6b1dad7a6f6db Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Sat, 14 Jul 2007 23:52:04 +0000 Subject: [PATCH] Define stripPrefix; fixes trac #1464 --- Data/List.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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. -- 1.7.10.4