Check the string if it contains the given substring and returns BOOLEAN

I am trying to come up with a haskell function that takes 2 string parameters. He then checks to see if the first line contains the second line as a substring. For example: "bring" contains the substring "in". If passed "bring" to ", the function should return true. Here is what I still have, but it really doesn't work. Partially works. I do not know how to assign a true value in recursive cases.

check::[Char]->[Char]->Bool
check [][]              =False
check _[]               =False
check []_               =False
check(x:xs)(y:ys)
 | y==x                 =True
 | otherwise            =check xs (y:ys)

main = do
print $ check "bring" "in"
+4
source share
2 answers

A direct implementation that does not use libraries or cool tricks could be:

substring :: String -> String -> Bool
substring (x:xs) [] = False
substring xs ys
    | prefix xs ys = True
    | substring xs (tail ys) = True
    | otherwise = False

prefix :: String -> String -> Bool
prefix [] ys = True
prefix (x:xs) [] = False
prefix (x:xs) (y:ys) = (x == y) && prefix xs ys

"" ( , ?): , .

, .

+4

, , , :

check::[Char]->[Char]->Bool
check [][]              =False
check _[]               =False
check []_               =False
check(x:xs)(y:ys)
 | y == x               =True -- this line
 | otherwise            =check xs (y:ys)

main = do
print $ check "bring" "in"

, :

check::[Char]->[Char]->Bool
check l s = check' l s True where
    check' _ [] h          = True
    check' [] _ h          = False
    check' (x:xs) (y:ys) h = (y == x && check' xs ys False) || (h && check' xs (y:ys) h)

main = do
print $ check "bring" "in"

, , .

, , , :

check::Eq a => [a]->[a]->Bool
check l s = check' l s True where
    check' _ [] h          = True
    check' [] _ h          = False
    check' (x:xs) (y:ys) h = (y == x && check' xs ys False) || (h && check' xs (y:ys) h)

main = do
print $ check "bring" "in"
+3

All Articles