user2352241
user2352241

Reputation: 81

ocaml mutual recursion error

I am new to Ocaml and want help with recursive function defined on a recursive data type. I have defined a data type as follows

    type 'a slist = S of 'a sexp list
    and
    'a sexp = Symbol of 'a | L of 'a slist

The function(subst) I'm writing checks for a symbol a in the defined slist and substitutes it by b. For ex subst 1 10 S[ 1; 4; S[L[3; 1;]; 3];] returns S[10; 4; S[L[S[3; 10;]]; 3;] . My code is as follows

    let rec subst a b sl = match sl with
    S s -> match s with
          [] -> []
          | p :: q -> match p with
                Symbol sym -> if sym = a then S[b] :: (**subst** a b S[q]) else
                                 S[p] :: (subst a b S[q])
                L l -> (subst a b l) :: (subst a b S[q])
    | L lis -> subst a b lis;;

I am getting the error :

This function is applied to too many arguments; Maybe you forgot a ';'

Please help

Upvotes: 0

Views: 201

Answers (1)

Thomash
Thomash

Reputation: 6379

Your type can be defined in a simpler way, you don't need slist:

type 'a sexp = Symbol of 'a | L of 'a sexp list

Your problem is that subst a b S[q] is read as subst a b S [q], that is the function subst applied to 4 arguments. You must write subst a b (S[q]) instead.

Upvotes: 1

Related Questions