By design, OCaml programs need no type annotations: their syntax is enough to infer their type. In practice, though, facing a complicated type-error message, a judicious type annotation can go a long way towards understanding this error message. Also, other programming languages such as Haskell and Gallina require type annotations. And in the module that follows the present one (namely Introduction to Data Structures and Algorithms), all OCaml programs are written with type annotations, so that instead of writing:
let list_append xs_given ys_given =
let rec visit xs =
match xs with
| [] ->
ys_given
| x :: xs' ->
x :: visit xs'
in visit xs_given;;
one writes:
let list_append (xs_given : 'a list) (ys_given : 'a list) : 'a list =
let rec visit (xs : 'a list) : 'a list =
match xs with
| [] ->
ys_given
| x :: xs' ->
x :: visit xs'
in visit xs_given;;
In other words, all the formal parameters and all the results of each named function are annotated with their expected type.
In this light, you are asked to annotate all the formal parameters and all the results of all the functions in the accompanying file.
Vigfus: There is nothing challenging in this exercise, right?Mimer: Right, it is a preparatory one for a subsequent module.Loki: You mean there is a life after Intro to CS?Brynja: There is, Loki, there is. It’s called “Life after Birth”.Halcyon: The birth of computer scientists, that’s us!Harald and Alfrothul: Aww...
None.
Alfrothul: Figures.Harald: Figures what?Alfrothul (with a straight face): None is an option.
Characterize the effect of Foo_maker, Bar_maker, and Baz_maker in the resource file for the present lecture note.
In a binary tree, every leaf is connected to the root of this tree by a “path”. This path has a length: for example, in a tree that consists of one leaf, the path from the root to this leaf has length 0.