Algorithm Ocaml-正则表达式的偏导数
我得到了这个密码:Algorithm Ocaml-正则表达式的偏导数,algorithm,types,ocaml,Algorithm,Types,Ocaml,我得到了这个密码: type regexp = | V (* void *) | E (* epsilon *) | C of char (* char *) | U of regexp * regexp (* a + b
type regexp =
| V (* void *)
| E (* epsilon *)
| C of char (* char *)
| U of regexp * regexp (* a + b *)
| P of regexp * regexp (* a.b *)
| S of regexp (* a* *)
;;
...
module ReS = Set.Make (struct
type t = regexp
let compare = compare
end)
(* module/type for pairs of sets of regular expressions *)
module RePS = Set.Make (struct
type t = ReS.t * ReS.t
let compare = compare
end)
(*module/type for set of chars *)
module CS = Set.Make(Char)
let ewps = ReS.exists ewp;;
let atmost_epsilons = ReS.for_all atmost_epsilon;;
let infinitys = ReS.exists infinity;;
let rigth_concat s = function
| V -> ReS.empty
| E -> s
| r -> ReS.map (fun e -> P (e,r)) s
;;
let ( *.* ) = rigth_concat;;
(* partial derivative of a regular expression *)
let rec pd a re = function
| V | E -> ReS.empty
| C b when b=a -> ReS.singleton E
| C b -> ReS.empty
| U (r, s) -> ReS.union (pd a r) (pd a s)
| P (r, s) when ewp a -> ReS.union ((pd a r) *.* s) (pd a s)
| P (r, s) -> (pd a r) *.* s
| S r as re -> (pd a r) *.* re
;;
let rec unions f s =
ReS.fold (fun re acc -> ReS.union (f re) acc ) s ReS.empty
;;
let rec pds a s = unions (pd a) s;;
let rec pdw (sr: ReS.t) = function
| [] -> sr
| a::tl -> pds a (pdw sr tl)
;;
我检查了返回值的类型,我认为它们是正确的,但它返回了以下错误,我不确定原因
此表达式的类型为regexp->ReS.t,但表达式为
应为ReS.t类型
在有错误的行中的函数“pd”中
| U (r, s) -> ReS.union (pd a r) (pd a s)
我相信您的问题是由于
函数
提供了一个隐式参数。这句话:
function None -> 0 | Some x -> x
是一个只有一个参数的函数。因此,在您的例子中,您定义了pd
有三个参数。在我看来,你希望它有两个参数
您可能可以将
函数…
改为将re与
匹配。或者您可以删除显式re
参数,并使用函数中隐式的参数
我相信您的问题是由函数
提供隐式参数造成的。这句话:
function None -> 0 | Some x -> x
是一个只有一个参数的函数。因此,在您的例子中,您定义了pd
有三个参数。在我看来,你希望它有两个参数
您可能可以将
函数…
改为将re与
匹配。或者您可以删除显式的re
参数,并使用函数中隐式的参数我只删除标记“function”并输入“match re with”?是的,应该可以。您也可以只删除第一行上的“re”。请注意,您现在没有在代码中的任何位置使用此参数re
。我只删除标记“function”并输入“match re with”?是的,应该可以。您也可以只删除第一行上的“re”。请注意,您现在没有在代码中的任何位置使用此参数re
。