From 7a4646e797aebed4de09c94e634c7cd7c9c91f25 Mon Sep 17 00:00:00 2001 From: John Goerzen Date: Thu, 31 Aug 2006 15:15:56 +0000 Subject: [PATCH] New function isInfixOf that searches a list for a given sublist Example: isInfixOf "Haskell" "I really like Haskell." -> True isInfixOf "Ial" "I really like Haskell." -> False This function was first implemented in MissingH as MissingH.List.contains --- Data/List.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Data/List.hs b/Data/List.hs index da6c878..8b504f7 100644 --- a/Data/List.hs +++ b/Data/List.hs @@ -100,6 +100,7 @@ module Data.List -- ** Predicates , isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool , isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool + , isInfixOf -- :: (Eq a) => [a] -> [a] -> Bool -- * Searching lists @@ -266,6 +267,17 @@ isPrefixOf (x:xs) (y:ys)= x == y && isPrefixOf xs ys isSuffixOf :: (Eq a) => [a] -> [a] -> Bool isSuffixOf x y = reverse x `isPrefixOf` reverse y +-- | The 'isInfixOf' function takes two lists and returns 'True' +-- iff the first list is contained, wholly and intact, +-- anywhere within the second. +-- +-- Example: +-- +-- >isInfixOf "Haskell" "I really like Haskell." -> True +-- >isInfixOf "Ial" "I really like Haskell." -> False +isInfixOf :: (Eq a) => [a] -> [a] -> Bool +isInfixOf needle haystack = isJust $ find (isPrefixOf needle) (tails haystack) + -- | The 'nub' function removes duplicate elements from a list. -- In particular, it keeps only the first occurrence of each element. -- (The name 'nub' means \`essence\'.) -- 1.7.10.4