Reputation: 128
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.
Upvotes: 1
Views: 141
Reputation: 5385
it seems you're trying to use c-style function call syntax. for a c-style function, e.g.
// defining the function
int plus(int a, int b)
{
return a + b;
}
// elsewhere, calling the function
plus(a, b); // brackets surrounding only the arguments, comma separated
the equivalent Haskell code will be
-- defining the function
plus :: Int -> Int -> Int
plus a b = a + b
-- elsewhere, calling the function:
(plus a b) -- brackets surrounding the function and the arguments, no commas
-- note, plus (a b) would be equivalent to c: plus(a(b));
Note, these brackets are only ever needed for disambiguation, and in this case they can be removed, leaving plus a b
.
In the following case, they would be needed:
plus(a, times(b, c));
This becomes:
(plus a (times b c))
Which is the same as:
plus a (times b c)
However it is not the same as:
plus a times b c
Upvotes: 2