我发现此代码用于评估互联网上OCaml中的表达式,并希望尝试了解其工作原理,但当我在编辑器中输入并运行它时,我收到以下错误:
type t =
| Integer of int
| Binary of binary * t * t
and binary =
| Add
| Sub
| Mult
| Div
type token =
| INTEGER of int
| ADD
| SUB
| MULT
| DIV
let rec eval = function
| Integer(k) -> k
| Binary(op, a, b) ->
(match op with
| Add -> ( + )
| Sub -> ( - )
| Mult -> ( * )
| Div -> ( / )) (eval a) (eval b)
let lexer s =
let open Str in
let split s =
let rec splitchar x l =
if x< 0 then l else splitchar (x-1) ( s.[x]:: l ) in
splitchar ( String.length s -1) []
|> List.map
(function
| "+" -> ADD
| "-" -> SUB
| "*" -> MULT
| "/" -> DIV
| _ -> failwith "lexer: Invalid token: %s" );;
人物280-282: | _ - &gt; failwith&#34; lexer:无效的令牌:%s&#34; );; ^^ 错误:语法错误
错误消息并没有多大帮助,我尝试了一些只会让事情变得更糟的变化。任何人都可以通过弄清楚语法错误是什么来帮助我开始吗?