Search⌘ K

Programming Challenges

Explore programming challenges that develop your skills in functional programming abstractions using OCaml. Learn to implement prime checks, optimize Fibonacci calculations, compose functions, apply higher-order functions, and create filtered accumulations. This lesson enhances your understanding of key concepts like recursion, function composition, and higher-order functions, enabling you to write efficient and elegant functional programs.

Challenge 1: Prime number

Write an OCaml function, is_prime: integer -> bool, that returns true if the input number is a prime number and false otherwise.

OCaml
(** [is_prime x] is [true] if [x] is a prime and [false] otherwise. *)
let is_prime x = false (* TODO: Replace the hard-coded value with your code here *)

Challenge 2: Naive Fibonacci

The Fibonacci series looks like this: 1 1 2 3 5 8 13 ...

Formally,

Fibonacci 0 = 0
Fibonacci 1 = 1
Fibonacci 2 = 1
Fibonacci n = Fibonacci (n-1) + Fibonacci (n-2) for n > 2

Write an OCaml function, fib: int -> int, that returns the n-th Fibonacci number where n ...