I need to write a function that check if a substring is within another string by using list comprehensions.
I'm using drop create a list of strings from a string to use isPrefixOf to compare the list of strings created to the substring.
Here is my code:
contains :: String -> String -> Bool
contains str substr = isPrefixOf substr (check str)
where
check :: String -> [String]
check str
|str==[] = []
|otherwise = [drop x str | x <- [0..length(str)-1]]
However, I get this error:
Tutorial2.hs:163:42: error:
* Couldn't match type `[Char]' with `Char'
Expected type: [Char]
Actual type: [String]
* In the second argument of `isPrefixOf', namely `(check str)'
In the expression: isPrefixOf substr (check str)
In an equation for `contains':
contains str substr
= isPrefixOf substr (check str)
where
check :: String -> [String]
check str
| str == [] = []
| otherwise = [drop x str | x <- [0 .. length (str) - 1]]
|
163 | contains str substr = isPrefixOf substr (check str)
I want to understand why I am getting this error and how do I fix it. I'm assuming it's because I'm giving isPrefixOf a list of a list [[a]] with my function check str and it doesn't take that?