第一,这是我的代码:我做区分OCaml中的功能和我有一些问题
module Problem1 = struct
type aexp =
| Const of int
| Var of string
| Power of string * int
| Times of aexp list
| Sum of aexp list
let diff : aexp * string -> aexp
= fun (exp, var) ->
match exp with
|Const a -> Const 0
|Var x -> if x = var then Const 1 else Var x
|Power (s, i) ->
if s = var then Times[Const i;Power (s, i - 1)] else Power (s, i)
|Times l ->
match l with
|h::t -> Sum[Times[diff (h, var);t];diff (t, var)]
|Sum m ->
match m with
|h::t -> Sum[diff(h, var); diff(t, var)];;
end
的解释器说,
Error: This variant pattern is expected to have type aexp list
The constructor Sum does not belong to type list
但我想要的符号M是一个aexp列表。 找不到错在哪里。
顺便说一句,这是你问在计算器的第五个问题,他们都没有一个公认的答案,因为人们为你做一些努力,这是一种不礼貌的,你可以通过接受你找到合适的答案,至少感谢他们。 – Lhooq
哦,我在stackoverflow中很新手,我不知道如何接受答案。对不起...谢谢你的好意.. :)) –