0
votes

I have this very simple recursive function, to add a branch to a tree from a list of char.

type tree = Node of bool ref * (char * tree) list ref

let rec create_branch lc = function
    [] -> Node(ref true, ref[])
    | x :: l -> Node(ref false, ref [(x, (create_branch l))])

I would expect this to create a tree containing only one branch, which would have a bunch of "false" nodes with char transitions until the final node which would be true.

But I'm getting an error:

Error: This expression has type (char * (char list -> tree)) list ref but an expression was expected of type (char * tree) list ref Type char list -> tree is not compatible with type tree

I think I understand that this means that my recursive call seems to, for some reason, get passed as a reference to the actual function (hence the (char list -> tree)), instead of its return value, which would simply be a tree.

I'd like to know what I am doing wrong?

1

1 Answers

3
votes

The function keyword creates a lambda expression, in which you can do multiple clauses of pattern matching.

The function you wrote takes two parameters, lc and the one being pattern matched.

You can replace function with match lc with, or delete lc to fix it.

This is the same problem with Ocaml: This expression has type 'a list * 'a list -> bool but an expression was expected of type bool, but I know it can be difficult to search for errors like that.