OCAML Course

Basic exercises

Go back

A bunch of simple exercises in OCaml, summarizing what you learned in the section "Basic concepts".

Exercise 1

Write a function returning true is a number is pairwise, false else.

val is_pair : float -> bool
let is_pair (number: float) : bool = 
	(* subtract 2 until we got 0 (so number%2=0) or 1 so number%2=1 *)
	let rec check_number (number: float) : bool = 
	    if number = 1.0
		then false
	    else if number = 0.0
			 then true
			 else check_number (number -. 2.0)
	in check_number number

As a side note, you already have an operator mod but it's not working on reals.