当前位置:  开发笔记 > 编程语言 > 正文

将OCaml代码转换为F#

如何解决《将OCaml代码转换为F#》经验,为你挑选了1个好方法。

我正在将一些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

有人知道我必须改变它以便我可以使用它吗?



1> Guy Coder..:

看起来你正试图翻译" 实用逻辑和自动推理手册 ".

您是否看到:现在可以使用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#:.如果您查看评论,您将看到我们三个人是如何开始这个项目的.

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有