OCAML Course

MCQ

Go back

A bunch of simple questions about OCaml (not quite an MCQ 😖), summarizing what you learned in the section "Basic concepts".


Question 1

What's the type of let my_function x = x * x?

Notice the * only usable on an int, so the function takes an int and return an int giving us int -> int

Question 2

What's the type of let my_function x y = x ** y?

Notice the ** only usable on two floats, so the function takes two floats and return a float giving us float -> float -> float. If you didn't know, x ** y is the exponentiation ($x^y$).

Question 3

What's the type of let my_function x = 5?

We don't know the type of x, but the return type is int, so we have 'a -> int.

Question 4

What's this code doing?

(* x is a number, power > 0 *)
let pow x power = 
	if power = 1
	then x
	else x * (pow x power-1)

It's not working.

  • The keyword rec is missing
  • We are missing a parenthesis around power-1, for OCaml we wrote (pow x power)-1

The code would do $x^{power}$ if it was working.


Question 5

Is a function like this $f_{n}(f_{n-1}(...f_{2}(f_{1}())...))$ terminal or nonterminal?

It's a terminal function! We are evaluating n before n+1. So we aren't postponing quite a lot of calculations.