Frequently Asked Questions
This page will be populated with reasonable questions, asked during the lecture or the exercises, or sent as feedback.How can I access course material from outside .uibk.ac.at?
Install a VPN.How do I get command line history and completion for ocaml
?
Just use rlwrap (installed on ZID machines). Either call
ocaml
as follows:
$ rlwrap ocaml
or set following alias (e.g., in your .bashrc
):
alias ocaml="rlwrap ocaml"
How to use ocamlbuild
?
There a different applications of ocamlbuild
:
- Compiling the file
prog.ml
as bytecode executable:
$ ocamlbuild prog.byte
- Compiling an interpreter
name.top
in which some modules are preloaded. First create a filename.mltop
containing one module per line that should be preloaded on startup. Second
$ ocamlbuild name.top
- Prepare a (hidden) file
.ocamlinit
containing the directive#cd "_build";
- Start the interpreter with
./name.top
How can I customize my own interpreter?
Every time an interpreter is started (either ocaml
or
something like name.top
from above) the current directory
is checked for the file .ocamlinit
. If it exists, its content
is executed as if you would have typed it in the interpreter. (Alternatively
the flag -init <file>
can be used to load the file
<file>
.)
This is for example useful to change to the directory _build
on startup (which is the default output location of ocamlbuild
).
The corresponding toplevel directive is #cd "_build";;
.
How to use the sourcecode archives?
Let's takew03.tgz
as example. Download the archive
$ wget http://cl-informatik.uibk.ac.at/teaching/ws09/fp/src/w03.tgz
extract the archive
$ tar -xzf w03.tgz
and change into the resulting directory w03
$ cd w03
To generate an OCaml interpreter in which all modules of the archive
are preloaded use
$ ocamlbuild w03.top
This command reads the file w03.mltop
(which you can extend by
your own modules of course). To start the interpreter use
$ rlwrap ./w03.top
now to use, e.g., the function of_string
of the module
Strng
type
Objective Caml version 3.11.0
# Strng.of_string;;
- : string -> char list = <fun>
How to define recursive functions?
For example
let rec even x =
if x = 0 then true
else odd (x - 1)
and odd x =
if x = 0 then false
else even (x - 1);;