0
votes

Hello guys I am trying to make a simple recursive method that simply takes in an accumulator and a target value. Then add one to the accumulator until it reaches the target value. I am very new to Ocaml but have a decent background in java.

I wrote up a quick snippet of code that shows what I want to do in java:

    public static int rec(int acc,int target) {
        if (acc == target) {
            return 0;
        } else {
            return rec(acc+1, target);
        }
    }

here is my attempt to mimic this code in Ocaml:

  let h_sequence x =
      let rec helper acc x = function
      | acc -> x
      | _ -> helper acc+1 x
      in
      helper 0 x;;

however I get the following error:

Error: This expression has type 'a -> 'b -> 'a
       but an expression was expected of type int

Here is how I am trying to understand the Ocaml code. So we have a function h_sequence that has a paramater x. Inside of the function h_sequence we have a recursive function named helper which has two paramaters acc and x. If acc = x then return x. Else start the recursion by passing in helper, add one to the acc and then return x. And after the in it is passing the helper function, setting 0 for the acc and setting x as x. Please let me know if my logic is off. Any help will be much appreciated!

edit new code:

  let h_sequence x =
      let rec helper acc x = 
        if acc = x then
            acc
        else
            helper (acc+1) x
      in
      helper 0 x;;

2
The recursive function helper actually has three "parameters": acc, x, and another "parameter" that that is matched using function. - Flux

2 Answers

1
votes

As @Flux says, your helper function has 3 parameters. You should also be aware that the pattern acc will match all values. Patterns consist essentially of constants, and any names appearing in a pattern will match (and be bound to) any corresponding value.

To compare x against acc you should just use an if statement.

Since you don't really want to use pattern matching, you can simplify things by removing function. You'll end up with something like this for the helper function:

let rec helper acc x =
    if x = acc then (* One of the cases *)
    else (* The other case *)
1
votes

... we have a recursive function named helper which has two parameters acc and x.

You are getting the error because the recursive function helper actually has three "parameters": acc, x, and another "parameter" that that is matched using function. The error message has given you this clue ('a -> 'b -> 'a).

Let's now look at the helper function:

let rec helper acc x = function
    | zzz -> x  (* Equivalent to `| _ -> x` *)
    | _ -> helper acc+1 x

Mistakes:

  1. function does pattern matching. Everything will match the pattern zzz, which means that the next pattern (| _ -> helper acc+1 x) is useless because it will never be matched. Note that I've changed the pattern's name from your acc to zzz in order to emphasize that function actually matching patterns.

  2. helper acc+1 x is equivalent to (helper acc) + 1 x, which is not what you want. It should be helper (acc+1) x instead.

Solution

let rec helper acc x = 
    if acc >= x then 0
    else helper (acc+1) x

You should use >= instead of = to handle the case where x is negative.