Im trying to find the index of a string in the context of a list that contains a certain substring, with no avail. Im unsure how one may pass the entirety of the untouched array as an argument to another function in a recursive function. Here is my approach:
find' :: [String] -> Maybe Int
find' [] = error "List is empty"
find' [x] = return maybe 0
find' (x:xs)
| "HU" `isInfixOf` x = return elemIndex x (x:xs)
| otherwise = checkTail
where checkTail = find' xs
The error is namely:
* Couldn't match type `[[String]] -> Maybe Int' with `Maybe Int'
Expected type: String -> [String] -> Maybe Int
Actual type: String -> [String] -> [[String]] -> Maybe Int
* The function `return' is applied to three arguments,
but its type `([String] -> [[String]] -> Maybe Int)
-> String -> [String] -> [[String]] -> Maybe Int'
has only four
In the expression: return elemIndex x (x : xs)
In an equation for find':
find' (x : xs)
| "HU" `isInfixOf` x = return elemIndex x (x : xs)
| otherwise = checkTail
where
checkTail = find' xs
| "HU" `isInfixOf` x = return elemIndex x (x:xs)
^^^^^^^^^^^^^^^^^^^^^^^^^
I dont really understand the error and why it doesnt match, because in both cases im returning a maybe int. However as I've mentioned im not sure if (x:xs) actually means the entire list for the argument. Just to clarify, im trying to find the index of the string from a list of strings.
returnandmaybe. They are both standard functions in Haskell, but I strongly suspect neither does what you think it does. (In particular byreturn maybe 0I think you just meanJust 0) - Robin Zigmond