[Haskell-cafe] Re: how can this code be less?

Christian Maeder Christian.Maeder at dfki.de
Fri Apr 25 07:34:58 EDT 2008


You may want to study the code form Data.List first:

isInfixOf               :: (Eq a) => [a] -> [a] -> Bool
isInfixOf needle haystack = any (isPrefixOf needle) (tails haystack)

cetin tozkoparan wrote:
> I wrote this code and Can it be less?
> [2,4,5] list is sub list of [3,7,*2,4,5*,9] list and return True but
> not of [3,7,*4,2,5*,9] list ; return False
> 
> sublist :: Eq a => [a] -> [a] -> Bool
> sublist [] _     = True
> sublist (_:_) []   = False
> sublist (x:xs) (y:ys)
>   | x == y      =  if isEqual (x:xs) (y:ys) == False
>                         then sublist (x:xs) ys
>                         else True
>   | otherwise   = sublist (x:xs) ys 
> 
> 
> isEqual :: Eq a => [a] -> [a] -> Bool
> isEqual [] _     = True
> isEqual (_:_) [] = False
> isEqual (x:xs) (y:ys)
>   | x==y     = isEqual xs ys
>   | otherwise    = False

isEqual is not needed, because "Eq" provides "==" over lists, too.

HTH Christian



More information about the Haskell-Cafe mailing list