-1

In Haskell we can enter multi-line code at the terminal, by enclosing it between " :{ " and " :} ". For example, typing

> :{ main = do
> print("Hello") :}

in ghci, we can then call main. How can we do this in Ocaml on utop?

user65526
  • 685
  • 6
  • 19
  • 2
    Just type in the expression, using enter to create a new line. It is evaluated only after entering the termination sequence `;;`. – glennsl Dec 16 '21 at 21:05
  • 1
    In utop, be aware that the up and down arrows keys will cycle through your history, rather than moving through the current expression. However, you can use the left and right arrow keys to move the cursor through your current expression for the purpose of editing. This works across multi-line expressions. – Chris Dec 16 '21 at 21:09
  • 1
    There are keyboard shortcuts for moving the cursor vertically as well: `Ctrl+p` (resp. `Ctrl+n`) moves the cursor up (resp. down) a line; also, `Ctrl+a` / `Ctrl+e` for moving to the start/end of the current line. Also related, you might be interested in [this](https://discuss.ocaml.org/t/double-semicolon-peculiarity/1261/16). – Maëlan Dec 16 '21 at 22:19

1 Answers1

1

The comments addressed this quite well, but just so there is an answer, there is no magic. Both the traditional OCaml toplevel (invoked with simply ocaml) and utop will read in until they find a terminating ;; token.

For example:

─( 17:36:11 )─< command 0 >───────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # print_endline 
"Hello, world!";;
Hello, world!
- : unit = ()
─( 17:36:11 )─< command 1 >───────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # let msg = "Hello, world!"
in
  print_endline msg;;
Hello, world!
- : unit = ()
Chris
  • 26,361
  • 5
  • 21
  • 42