Recursive function

I have a function x that sequentially calls two other functions f1 and f1 for eg. , passes the results of f1 to f2 and repeats the process by calling itself.

let requestHandler option1 = 
  Printf.printf "Inside RequestHandler : %s\n" option1 ;
  option1
 
let valueStore option2 =
  Printf.printf "Inside valueStore : %s\n" option2
  
let rec main =
  (* print_endline(res); *)
  let vall1 = requestHandler "stringVar" in 
  valueStore vall1 ;
  main

But this throws error .

Entering directory '/home/ocaml/'
File "/client.ml", lines 13-15, characters 2-6:
13 | ..let vall1 = requestHandler "stringVar" in 
14 |   valueStore vall1 ;
15 |   main
Error: This kind of expression is not allowed as right-hand side of `let rec'

You can’t have a function with zero arguments. You must use unit to have this effect.
Define main as:

let rec main () = ...

and call it like this:

main ()
1 Like

After defining the function with unit , it compiles and executes too but doesn’t print anything, and also it terminates which should not happen as it is being called recursively.

Most likely you forgot to call main. If you are compiling add this as the last line:

let () = main ()
1 Like