Exercises for Week 13

Exercise 00

  1. At the top right and at the bottom right of the present page, there is a clickable word, “index”, to access the index of the current version of the lecture notes. Click on it and then peruse the index, making sure that its entries make sense to you (otherwise, click on them to check them out).
  2. The lecture notes start with updates (Chapter Lecture Notes for Intro to CS, updates). Make sure to check them out regularly, as they reflect the development of the lecture.
  3. Do take the time to peruse the lecture notes of this week and to reproduce their OCaml content.

Mandatory exercises from Week 11

  • Exercise 23: filtering in elements that satisfy a given predicate in a given stream
  • Exercise 25: playing with the stream of Fibonacci numbers
  • Exercise 26: playing some more with the stream of Fibonacci numbers

Mandatory exercises from Week 12

  • Exercise 02: printing the characters of a string
  • Exercise 03: printing the prefixes and the suffixes of a string
  • Exercise 14: finding the missing digit in a string of 9 distinct digits

Mandatory exercise in preparation for Introduction to Data Structures and Algorithms

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...

Optional exercises

None.

Alfrothul: Figures.
Harald: Figures what?
Alfrothul (with a straight face): None is an option.

Exercise 15

Characterize the effect of Foo_maker, Bar_maker, and Baz_maker in the resource file for the present lecture note.

Exercise 16

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.

  1. Implement a function that computes the length of the longest path and the length of the shortest path in that tree.
  2. Implement a function that computes the length of the longest path in that tree. Is there another name for this length?
  3. Implement a function that computes the length of the shortest path in that tree. Is structural recursion the most efficient way to implement this function?

Resources

Version

Added the exercises for Week 13 [16 Apr 2022]

Created [02 Apr 2022]