Grozav Alex Ioan
Grozav Alex Ioan

Reputation: 1559

OCaml simple interface based on user text input

I'm trying to create a very simple user interface for my Binary Search Tree project.

I've done all the necessary functions for the BST but my problem is that I can't get a hold on how to reference to a tree variable inside the program so that I can update it and pass it through each program call.

let rec interface endCondition tree =
     let option = read_int () 
     in 
       if option = endCondition then 
         let () = print_string "Thank you for using the program!" in
         let () = print_newline ()
         in print_newline ()
       else 
         let () = 
           if option = 1 then 
             let value = read_int  () 

             (* line I'm having problems with *)
             in let tree = insert tree value

             in let () = Printf.printf "Inserted Node: %d" value 
             in print_newline () 
           else if option = 2 then 
             let () = print_string "Search Node:"
             (* search code here *)
             in print_newline () 
           else 
             let () = print_string "Lower"
             in print_newline ()
         in interface endCondition tree;;

Whenever I use the let function it creates a new variable. How can I use the tree passed as parameter?

Thank you very much!

Upvotes: 0

Views: 149

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

You need to pass the new tree up farther so you can get at it at the end, something like this:

else
    let tree' =
        if option = 1 then let ... in insert tree value
        else if option = 2 then let ... in tree
        else let .... in tree
     in interface endCondition tree'

Upvotes: 2

Related Questions