I need to pass two list as command line arguments in ocaml. I used the following code to access it in the program.
let list1=Sys.argv.(1);;
let list2=Sys.argv.(2);;
I need to have the list1 and list2 as list of integers. I am getting the error
This expression has type string but an expression was expected of type int list
while processing. How can I convert that arguments to a list of integers. The arguments are passed in this format [1;2;3;4] [1;5;6;7]
As Sys.argv is a
string array
, you need to write your own transcription function.I guess the simplest way to do this is to use the Genlex module provided by the standard library.
Depending on the OCaml flavor you want to use, some other library may look more interesting. If you like yacc, Menhir may solve your problem in a few lines of code.
Sys.argv.(n)
will always be a string. You need to parse the string into a list of integers. You could try something like this:Of course this doesn't check the input for correct form. It just pulls out sequences of digits by brute force. To do better you need to do some real lexical analysis and simple parsing.
(Maybe this is obvious, but you could also test your function in the toplevel (the OCaml read-eval-print loop). The toplevel will handle the work of making a list from what you type in.)