Convert OCaml code to F #

I am converting some OCaml code to F # having a problem with OCaml let...and... that only exists in F # using a recursive function. I have an OCaml code:

 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" 

which I want to use in these two methods:

 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 

Does anyone know how I can change it so that I can use it?

+5
source share
2 answers

Looks like you're trying to translate a Practical Logic and Auto-Justification Handbook .

You saw: Now the version of the book code F # has appeared ! Thanks to Eric Toucher, Jack Pappas and An-Dang Feng.

You need to watch 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 

I asked a lot of questions here, working on translation and prefixes using Converting OCaml to F#: If you look in the comments, you will see how the three of us worked on this project.

+7
source

You can simply write:

 let explode s = [for c in s -> string c] let matches str strc = let eStr = explode str List.contains strc eStr let space = matches " \t\n\r" let punctuation = matches "() [] {}," let symbolic = matches "~' !@ #$%^&*-+=|\\:;<>.?/" let numeric = matches "0123456789" let alphanumeric = matches "abcdefghijklmopqrstuvwxyz_'ABCDEFGHIJKLMNOPQRSTUVWXYZ" 

F # tends to be written using easy rather than verbose syntax, so you usually don't need to use the in , begin and end . See https://msdn.microsoft.com/en-us/library/dd233199.aspx for more details on the differences.

Personally, I would probably reorganize all these string -> bool functions into active templates, for example:

 let (|Alphanumeric|_|) str = match matches "abcdefghijklmopqrstuvwxyz_'ABCDEFGHIJKLMNOPQRSTUVWXYZ" str with |true -> Some str |false -> None let (|Symbolic|_|) str = match matches "~' !@ #$%^&*-+=|\\:;<>.?/" str with |true -> Some str |false -> None 

Then you can map the image, for example:

 match c with |Alphanumeric _ -> // alphanumeric case |Symbolic _ -> // symbolic case |_ -> // other cases 
+3
source

Source: https://habr.com/ru/post/1239947/


All Articles