Reputation: 85
New to OCaml Here.
Basically trying to compile a piece of code mostly with two functions, but I can't compile it with type error on line 9, column characters 26-27, saying:
"Error: This expression has type t but an expression was expected of type string""
Basically, the parse function called on line 8 is expecting type string, but I can't figure out why.
Type of the sexp argument:
type sexp = Atom of string | List of sexp list
code:
open Sexplib.Sexp
let rec parse_bindings bindings =
match bindings with
| [] -> []
| first::rest ->
match first with
| List([Atom(name); e]) ->
[(name, parse e)] @ (parse_bindings rest)
let rec parse sexp : expr =
match sexp with
| Atom(s) ->
(* some code *)
| List(sexps) ->
match sexps with
| (* some code *)
| [Atom("let"); List(bindings_sexp); e2] ->
let binding_expr = parse_bindings bindings in
ELet(binding_expr, parse e2)
| _ -> failwith "foo"
Upvotes: 0
Views: 245
Reputation: 66808
The code as you give it won't compile because parse
is referenced on line 9, but isn't defined until later.
To define two mutually recursive functions you need to use let rec ... and ...
:
let rec f x = (* definition of f, which calls g *)
and g x = (* definition of g, which calls f *)
Since the later definition of parse
isn't visible on line 9, the name must refer to some previous definition. Maybe there's a function named parse
defined in the Sexp
module. (This is one reason to use open
carefully.)
Upvotes: 1