I am having a problem, I have a list like this [["A1","A2","A3"],["B1","B2","B3"],["A1","B3""C3"]]
I want to return a list if every string contains 2, the desired return should be as follows:
[["A1","A2","A3"],["B1","B2","B3"]]
my recent approach is
isIn :: Int -> [String] -> Bool
isIn a [] = False
isIn a (x:xs)
| show a == x = True
| otherwise = isIn a xs
And I tested it using the list above it gave me False, what did I wrong? How should I fix this?
By the type definition isIn :: Int -> [String] -> Bool
I assume that the function is iterating the list of strings (for example ["A1","A2","A3"]
).
Therefore, the problem is in show a == x
. x
is equal to "A1"
, "A2"
, "A3"
; show 2
is "2"
; but "2"
is not equal to any of these strings.
You'll need to use the function that verifies that a string contains
"2", not equal
to "2" (there's elem function)
Or implement it:
isInString :: Int -> [Char] -> Bool
isInString a [] = False
isInString a (x:xs)
| intToDigit a == x = True
| otherwise = isInString a xs
And then use it in isIn
:
isIn :: Int -> [String] -> Bool
isIn a [] = False
isIn a (x:xs)
| isInString a x = True
| otherwise = isIn a xs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.