我正在将某些OCaml代码转换为F#,而OCaml let...and...
则存在问题,该问题仅通过使用递归函数存在于F#中。
我有给定的OCaml代码:
let matches s = let chars = explode s in fun c -> mem c chars
let space = matches " \t\n\r"
and punctuiation = matches "() [] {},"
and symbolic = matches "~'!@#$%^&*-+=|\\:;<>.?/"
and numeric = matches "0123456789"
and alphanumeric = matches "abcdefghijklmopqrstuvwxyz_'ABCDEFGHIJKLMNOPQRSTUVWXYZ"
我想在这两种方法中使用:
let rec lexwhile prop inp = match inp with
c::cs when prop c -> let tok,rest = lexwhile prop cs in c+tok,rest
|_->"",inp
let rec lex inp =
match snd(lexwhile space inp)with
[]->[]
|c::cs ->let prop = if alphanumeric(c) then alphanumeric
else if symbolic(c) then symbolic
else fun c ->false in
let toktl,rest = lexwhile prop cs in
(c+toktl)::lex rest
有人知道我必须如何更改它才能使用它吗?
最佳答案
看来您正在尝试翻译“Handbook of Practical Logic and Automated Reasoning”。
您看到了吗:An F# version of the book code现在可用!感谢Eric Taucher,Jack Pappas和Anh-Dung Phan。
你需要看看intro.fs
// pg. 17
// ------------------------------------------------------------------------- //
// Lexical analysis. //
// ------------------------------------------------------------------------- //
let matches s =
let chars =
explode s
fun c -> mem c chars
let space = matches " \t\n\r"
let punctuation = matches "()[]{},"
let symbolic = matches "~`!@#$%^&*-+=|\\:;<>.?/"
let numeric = matches "0123456789"
let alphanumeric = matches "abcdefghijklmnopqrstuvwxyz_'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let rec lexwhile prop inp =
match inp with
| c :: cs when prop c ->
let tok, rest = lexwhile prop cs
c + tok, rest
| _ -> "", inp
let rec lex inp =
match snd <| lexwhile space inp with
| [] -> []
| c :: cs ->
let prop =
if alphanumeric c then alphanumeric
else if symbolic c then symbolic
else fun c -> false
let toktl, rest = lexwhile prop cs
(c + toktl) :: lex rest
在进行翻译时,我在这里询问了许多questions,并给它们加上了
Converting OCaml to F#:
前缀。如果您在评论中查看,您将看到我们三个人如何参与该项目。关于f# - 将OCaml代码转换为F#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34624251/