1

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.

2
  • 3
    Your mistake is in your recursive call to get_index. In the last line of get_index, you meant get_index (index + 1) char str, not get_index(index + 1 char str). And then the function works as expected! Commented Mar 25, 2017 at 19:20
  • Yes, it worked. Than you very much! Commented Mar 25, 2017 at 19:23

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I'd add that the outer parens are not required and should be omitted (this isn't Lisp!), unless plus a b is the argument to another function call.
I'll mention that they aren't necessary, but if this function is embedded in another function call then it is required for disambiguation, whereas no additional brackets are needed in this case in a c function call. Eg f(x, g(y)) => (f x (g y)) == f x (g y), but not f x g y

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.