Download the resource .ml file of the week in the appropriate subdirectory.
Open this file (C-x C-f), edit it at your leisure, and save it (C-x C-s).
Start OCaml (M-x run-ocaml), which splits your emacs window into 2 (C-x o to go to the other emacs window.)
In the OCaml window, type #use "file-of-the-week.ml";;, where file-of-the-week.ml is the name of the file of the week you are currently editing, and hit the Enter key.
The contents of this file is then processed by the OCaml processor as if you had interactively typed it yourself, as illustrated in this screenshot.
If the file you are loading contains errors, the OCaml processor stops at the first one and issues an error message together with the line number in the file where the error occurs. Go to the other emacs window (C-x o) and to the corresponding line (M-x goto-line enter, followed by the line number (and the Enter key)). Fix what needs to be fixed, save the file (C-x C-s), go back to the OCaml window (C-x o), and load the file again (M-p enter).
Problem: the file is downloaded elsewhere.
What to do: either move it to the appropriate subdirectory, or figure out how to specify where files should be downloaded.
Problem: when opening this file, the Tuareg mode is not activated.
What to do: talk to a peer tutor or the lecturer to install the Tuared mode properly.
Problem: the OCaml processor starts in another directory.
Option #1: specify an absolute path for the file to load with the #use command.
Option #2: change the current directory of the OCaml processor with #cd "absolute-name-of-the-subdirectory-of-the-week";;.
Option #3: if Option #2 doesn’t work, which seems to be the case on several MacBooks, tell OCaml where to download your file with #directory "absolute-name-of-the-subdirectory-of-the-week";;.
Problem: you fixed an error, but the very same error occurs when re-loading the file.
Explanation: you forgot to save the file after fixing the error.
The modus operandi described above is minimalistic, but it is simple and sufficient to get you to the other end of of the semester.
It is limited in that it assumes that your program resides in one file. Spreading a program across several files requires loading these files in the right order, or using the #use command in the file to load.
In the subsequent course Introduction to Data Structures and Algorithms, you will be presented another modus operandi that lifts this limitation.
Consider the problematic content of this .ml file:
(* week-03_a-problematic-dot-ml-file.ml *)
(* Introduction to Computer Science (YSC1212), Sem2, 2019-2020 *)
(* Olivier Danvy <danvy@yale-nus.edu.sg> *)
(* Version of Mon 03 Feb 2020 *)
(* ********** *)
let one = 1
and one = 2
and three = 3;;
(* ********** *)
let four = two + too;;
(* ********** *)
(* end of week-03_a-problematic-dot-ml-file.ml *)
"week-03_a-problematic-dot-ml-file.ml"
Let us load this file:
# #use "week-03_a-problematic-dot-ml-file.ml";;
File "week-03_a-problematic-dot-ml-file.ml", line 9, characters 4-7:
Error: Variable one is bound several times in this matching
#
The OCaml processor issues an error message about an expression that occurs on Line 9 of the file. The error is described in Section Declaring several global variables at once: when declaring several global variables, the names of these variables must be distinct. Action:
Loading the corrected file elicits the following answer from the OCaml processor:
# #use "week-03_a-problematic-dot-ml-file.ml";;
val one : int = 1
val two : int = 2
val three : int = 3
File "week-03_a-problematic-dot-ml-file.ml", line 14, characters 17-20:
Error: Unbound value too
Did you mean two?
#
The file was loaded until the next error in the file, which occurs on Line 14 and is a typo that is singled out with a useful error message. Action:
type C-x o to go to the other buffer, i.e., the .ml file;
type M-x goto-line followed by the Enter key, and then 14 followed by the Enter key to go to Line 14, where the error occurs;
type M-1 M-7 C-f (or alternatively ESC 1 7 C-f) to go to the point where the problem occurs;
type C-f C-d w to (1) move the cursor forward, (2) delete the offending character, i.e., o, and (3) insert the correct one, i.e., type w, resulting in the corrected line:
let four = two + two;;
type C-x C-s to save the content of the buffer in the file;
type C-x C-o to go to the other buffer, i.e., the OCaml toplevel;
type M-p to go back in the history to the #use command (once is enough, but twice would work too here since we are about to load the file for the third time), and hit the Enter key.
Loading the corrected file gives rise to the error-free interaction with the OCaml processor:
# #use "week-03_a-problematic-dot-ml-file.ml";;
val one : int = 1
val two : int = 2
val three : int = 3
val four : int = 4
- : string = "week-03_a-problematic-dot-ml-file.ml"
#
The content of the file is now accepted by the OCaml processor, and therefore no longer problematic, its name notwithstanding.
And last but not least, observe how through this interaction, the palms of your hands have peacefully remained on your keyboard.
Added Option #3 [04 Feb 2020]
Added the screenshot and the concrete example [03 Feb 2020]
Created [31 Jan 2020]