我正在将一些OCaml代码转换为具有OCaml问题的F#,该问题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
有人知道我必须改变它以便我可以使用它吗?
看起来你正试图翻译" 实用逻辑和自动推理手册 ".
您是否看到:现在可以使用F#版本的图书代码!感谢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
在处理翻译时,我在这里问了很多问题,并将它们作为前缀Converting OCaml to F#:
.如果您查看评论,您将看到我们三个人是如何开始这个项目的.