Programming Challenges

Let's solve coding challenges to practice dataflow programming with functions.

Challenge 1: Area of the largest circle

The following algebraic datatype represents a geometric shape. For our purposes here, we’ll only consider circles and rectangles.

type shape =  Circle of float
            | Rectangle of float * float

The following function calculates the area of a shape:

let rec area s = match s with
                 | Circle r -> 3.14 *. r *. r
                 | Rectangle (w, h) -> w *. h

Write an OCaml function, max_circle, that takes as an input a list of shapes and returns the area of the largest circle in the list.

For example:

max_circle [] = 0.
max_circle [Circle 1.; Rectangle (1., 2.); Circle 2.; Rectangle (2., 3.)] = 12.56

In this challenge, try to think of how you could define a dataflow diagram to solve the problem.

Get hands-on with 1400+ tech skills courses.