简体   繁体   English

Ocaml-(int * int * int)列表的字符串

[英]Ocaml - string to (int*int*int) list

Is in ocaml function which can parse string like this "[(1,2,3);(1,2,5);(2,3,4)]" into (int*int*int) list ? 在ocaml函数中,可以将像这样的字符串[[(1,2,3);(1,2,5);(2,3,4)]“解析为(int * int * int)列表吗? or do I have to write my own parsing function ? 还是我必须编写自己的解析函数?

Thanks 谢谢

Greg 格雷格

Well, it should not be too difficult with help of the Scanf module from the standard library -- you will be able to scan the triples easily and then only have to add a bit of logic to parse a list. 好吧,借助标准库中的Scanf模块,应该不会太困难-您将能够轻松扫描三元组,然后只需添加一些逻辑即可解析列表。

Alternatively you can take a look at deriving : 另外,您可以看一下推导

Extension to OCaml for deriving functions from type declarations. OCaml的扩展,用于从类型声明中派生函数。 Includes derivers for pretty-printing, type-safe marshalling with structure-sharing, dynamic typing, equality, and more. 包括用于漂亮打印,带有结构共享的类型安全编组,动态类型,相等性等的派生程序。

If you're willing to use a somewhat different format, you can have your parser for free. 如果您愿意使用略有不同的格式,则可以免费使用解析器。 Using the sexplib syntax extension, you can simply write: 使用sexplib语法扩展,您可以简单地编写:

type t = (int * int * int) list with sexp

and this will automatically define for you two functions, sexp_of_t and t_of_sexp . 这将自动为您定义两个函数sexp_of_tt_of_sexp Sexplib also provides multiple parsing functions. Sexplib还提供了多种解析功能。

The concrete syntax of s-expressions, however, is different. 但是,s表达式的具体语法是不同的。 Your example would be rendered: 您的示例将呈现:

((1 2 3) (1 2 5) (2 3 4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM