## Functional programming interview question

I think that examining the hiring process of a company you can understand a lot of what would be working there.

As *Joel Spolsky* wrote, you should only hire people who are *Smart and Get Things Done* and a good way to be sure that a candidate belongs to this category is testing his/her skills with a good programming exercise, one easy enough to be solved in 15 minutes but that requires the use of brain.

Starling Software clearly describes its interview process on a page of its website and proposes a couple of sample programs for the potential applicants. In the first problem you are asked to use Haskell to process a given file (obviously we will use F#):

The file input.txt contains lists of words, one per line, in two categories, NUMBERS and ANIMALS. A line containing just a category name indicates that the words on the following lines, until the next category name, belong to that category. Read this file as input (on stdin) and print out a) a sorted list of the unique animal names encountered, and b) a list of the number words encountered, along with the count of each. Feel free to chose your output format.

The algorithm to solve the exercise is easy: we read the file line by line and remember the current category (NUMBERS or ANIMALS) in order to add the next words to the appropriate list. When the file is over, we filter duplicates and sort the list of animals and group the numbers together with their counts.

The only problem is knowing how to manage the concept of *state* of the application in a functional way. In the imperative paradigm you define a variable to keep the state and change its value when you find a new category in the input file. In functional programming you don't use state variables instead you use function parameters and recursive calls:

open System open System.IO let animals_and_number filename = let rec process_line lines category animals numbers = match lines with | [] -> (animals, numbers) | x::xs -> match x with | "NUMBERS" -> process_line xs "NUMBERS" animals numbers | "ANIMALS" -> process_line xs "ANIMALS" animals numbers | x -> match category with | "NUMBERS" -> process_line xs category animals (x :: numbers) | "ANIMALS" -> process_line xs category (x :: animals) numbers | _ -> process_line xs category animals numbers let all_lines = File.ReadAllLines(filename) |> Seq.to_list process_line all_lines "" [] [] let filename = "input.txt" let (animals, numbers) = animals_and_number filename let sorted_animals = animals |> Seq.distinct |> Seq.sort |> Seq.to_list let counted_words = numbers |> Seq.countBy (fun x -> x) |> Seq.to_list printf "Animals: %A\n" sorted_animals printf "Numbers: %A" counted_words

The recursive *process_line* function has four parameters: the list of lines to be processed, the current category (initially an empty string) and the two lists of animals and numbers found so far.

For each new line we first check if it represents one of the categories. In this case we have to *change state*, i.e. discard the element and recursively call the same function with the correct category parameter.

If the element processed is not a category we only have to add it to the animals or number list, according to the value of the category parameter.

At the end of the *animals_and_number* function (when *lines* is empty) we return a tuple made of the two lists created.

The rest of the job is calling some standard library functions to filter duplicates, sort and count the elements of the sequences.

## Even Digits Multiple Of Nine

The mathematical exercise we are going to solve comes from mathschallenge.net and can be summarized in a single sentence:

Find the smallest multiple of nine containing only even digits.

We just need a function to **check if a number contains only even digits** and then pass to it the multiples of nine.

This algorithm can be translated into the following code:

#light open System let isEven n = (n % 2 = 0) let rec allEvenDigits n = match n with | n when n < 10 -> isEven n | n -> (isEven (n % 10)) && (allEvenDigits (Int32.div n 10)) let nums = 9 |> Seq.unfold (fun i -> Some (i, i + 9)) let answer = Seq.find allEvenDigits nums

The *isEven* function is just a "shortcut" for the modulus operator and it is used inside *allEvenDigits*, where it is applied to each digit of the given number.

Then, we have to generate the multiples of nine, but we don't know when to stop, so we need an **infinite sequence**, that can be produced by the *Seq.unfold* function.

At the end we use *Seq.find*, which takes a boolean function (*allEvenDigits*) and a sequence (*nums*), and returns the first element of the sequence for which the given function returns "true", i.e. **the smallest multiple of nine containing only even digits**, that is exactly what we had to find.

## Sorting odd and even numbers in F#

An Italian Microsoft Evangelist posted today a small programming exercise on his blog, presenting the solution with **LINQ**.

The exercise says (translated from Italian by me):

Given a list of unordered numbers (for instance 1, 7, 9, 2, 3, 4, 3, 4, 2, 3, 4, 5, 2, 0, 9), create a new list with all even numbers first and then all the odd ones.

The proposed solution also goes a little further, showing not only how to split the original list into odd and even numbers, but also putting them in the correct order, thus returning "**022244413335799**".

The following is the **C# code** used:

List<int> elenco = new List<int> { 1,7,9, 2, 3, 4, 3, 4, 2, 3, 4, 5, 2, 0, 9 }; var pariEdispari = elenco.OrderBy(s => s % 2 != 0); var pariEdispariOrdinati = elenco.OrderBy(s => s % 2 != 0).ThenBy(s => s); foreach (var item in pariEdispariOrdinati) { Console.WriteLine(item); }

And now let's compare it with an **F# approach**:

#light let numbers = [ 1; 7; 9; 2; 3; 4; 3; 4; 2; 3; 4; 5; 2; 0; 9 ] let result = numbers |> List.sort Int32.compare |> List.partition (fun x -> x % 2 = 0) print_any ((fst result) @ (snd result))

As you can easily see, everything is done at **line 4**, where we apply twice the **pipeline operator** (**|>**).

This operator allows to **chain functions**, passing the output of one of them to the next one.

The same line could be written as:

List.partition (fun x -> x % 2 = 0) (List.sort Int32.compare numbers)

Line 6 is only used to print the result, but it has to take into account that the output of *List.partition* is a * tuple*, so we have to concatenate the two elements that can be retrieved with the

*fst*(first) and

*snd*(second) functions.

The **@ operator** actually concatenates the two lists.

I'm not sure that this is the best (and most elegant) solution, but I think that it is way ahead than any imperative solution, do you agree with me?