1
votes

Can someone explain the syntax used for when you have nested functions?

For example I have a outer and an inner recursive function.

let rec func1 list = match list with
[] -> []
|(head::tail) ->
let rec func2 list2 = match list2 with
...
;;

I have spent all day trying to figure this out and I'm getting a ever tiring "Syntax error".

1
can you provide a full example? - ivg
Unfortunately I cannot show a a full example. The above was just a generic recursive within a recursive. Maybe a simple question for the OCaml experts would be how do I move back to the outer function. So, let's say that we have a conditional in the inner function and after evaluating where it's true or false and execute the single statement I want to move back out to func1 (above). How would I break out of the inner recursive block? - ButtahNBred
You shouldn't ask questions in comments. It's too hard to follow. An OCaml function is an expression. There's no moving or breaking, as there is (in some sense) in an imperative language. You have to show some kind of code to get a more useful answer. - Jeffrey Scofield

1 Answers

3
votes

You don't show enough code for the error to be obvious.

Here is a working example:

# let f x =
      let g y = y * 5 in
      g (x + 1);;
val f : int -> int = <fun>
# f 14;;
- : int = 75

Update

Something that might help until you're used to OCaml syntax is to use lots of extra parentheses:

let rec f y x =
    match x with
    | h :: t -> (
        let incr v = if h = y then 1 + v else v in
        incr (f y t)
    )
    | _ -> (
        0
    )

It's particularly hard to nest one match inside another without doing this sort of thing. This may be your actual problem rather than nested functions.