Functional Programming

bachelor program

VO2 + PS1  WS 2016/2017  703024 + 703025

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

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 as bytecode executable:
    $ ocamlbuild prog.byte
  • Compiling an interpreter in which some modules are preloaded. First create a file name.mltop containing one module per line that should be preloaded on startup. Second
    $ ocamlbuild
  • Prepare a (hidden) file .ocamlinit containing the directive #cd "_build";
  • Start the interpreter with ./

How can I customize my own interpreter?

Every time an interpreter is started (either ocaml or something like 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 take w03.tgz as example. Download the archive
  $ wget
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
This command reads the file w03.mltop (which you can extend by your own modules of course). To start the interpreter use
  $ rlwrap ./
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);;