Ocaml - string in (int * int * int) list

Is there an ocaml function that can parse a string like this? [(1,2,3); (1,2,5); (2,3,4)] "in the (int * int * int) list? Or do I need to write my own parsing function?

thanks

Greg

+7
source share
2 answers

Well, this should not be too complicated with the Scanf module from the standard library - you can scan triples easily, and then you need to add some logic to analyze the list.

Alternatively, you can take a look at deriving :

Extend to OCaml to derive functions from type declarations. Includes derivatives for fairly printable, safe sorting by type with sharing structure, dynamic typing, equalities, etc.

+4
source

If you want to use a slightly different format, you can get your parser for free. Using the sexplib syntax extension, you can simply write:

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

and it will automatically detect two functions for you: sexp_of_t and t_of_sexp . Sexplib also provides several parsing features.

The specific syntax of s-expressions, however, is different. Your example will display:

 ((1 2 3) (1 2 5) (2 3 4)) 
+4
source

All Articles