1

What's the problem with the code below? I'am struggling a lot with knowing when to use ';' or ';;' or use begin end in OCaml. Here i need to read some edges and insert into graph but i need to link this with rest of program so that it can use g(a graph) with all the edges. When i do this it says Error: Syntax error

let i = ref n in
while !i > 0 do
( 
    let pair = read_edge Scanning.stdin in
    let g = insert_edge (fst pair) (snd pair) g in
    i := !i - 1 
)
done in (* giving error in this line *)

let rec do_stuff l = 
  match l with
  | [] -> ()
  | h::t -> print_int h;do_stuff t in

( * more functions)

1 Answer 1

3

in is part of the syntax of let (it's "let" pattern "in" expression). The syntax of a while loop is simply "while" expression "do" expression "done", so there's no in in there.

To make your code compile you can replace in with a ;, so it executes the while loop followed by the let expression after it. However it seems strange to me that you'd have a while loop between a bunch of function definitions.

Also note that in the loop you do let g = ..., but then you're not using g anywhere in the let's body. So that binding accomplishes nothing.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, <expr>; is only syntactic sugar for let () = <expr> in if I'm not mistaken.
My idea of let g = .... was to keep updating g so that when while loop finishes it can be used by the rest of the program. I wanted to link it with done in, if i can't do it how can i solve this? I think i saw done in before but now that i think of it it was in a for loop not in a while loop.
@power_output The only way you'd see done in would be if the loop was used as the value in a let expression, i.e. let something = while cond do ... done in .... Anyway, let creates a new variable - it does not affect the value of an existing variable (which you can't do). If you want to be able to reassign something, you need a ref (or anything with a mutable member).
Can you tell me what is usual structure of OCaml program? I want to do dfs on a graph, reading input from stdin. Should i declare all the functions that i will use first and then at the end start reading from stdin to finally call a function that will start everything? I'am going to have to read lines from stdin in a while loop, each line at some point has the edge to insert which mean graph is updated with each line. If i use let g = add_new_edge ... i'am creating variables each time should i then use ref? I feel like it's bad practice in OCaml but i don't know better.
@power_output "Should i declare all the functions that i will use first and then at the end start reading from stdin to finally call a function that will start everything?" Yes, that sounds like the proper way to do it. As for updating the graph, you should probably use a recursive function rather than a while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.