I am trying to write an equivalent function to Javascript's "indexOf".(getting the index of a character in a string), but I am having problems when calling the recursive function.
This is the error:
Couldn't match expected type `Int'
with actual type `a0 -> [a0] -> Int'
In the return type of a call of `get_index'
Probable cause: `get_index' is applied to too few arguments
In the expression: get_index (index + 1 char str)
In an equation for `get_index':
get_index index char str
| index < 0 || index >= (length str) = - 1
| (str !! index) == char = index
| otherwise = get_index (index + 1 char str)
Failed, modules loaded: none.
This is my code:
index_of char str =
get_index 0 char str
get_index index char str
| index < 0 || index >= (length str) = -1
| (str !! index) == char = index
| otherwise = get_index(index + 1 char str)
First function's purpose is solely to call the recursion with the index parameter, nothing more, the problem I have is in the second function, the recursion.
get_index. In the last line ofget_index, you meantget_index (index + 1) char str, notget_index(index + 1 char str). And then the function works as expected!