F# - Find the latest appearance of of a new item in a list

Post date: Dec 1, 2011 10:39:58 PM

This was an interesting problem that i wanted to solve using F#.

The problem is to find the latest appearance of a new customer in a list. The list contain an entry for each purchase that a customer have made, sorted on the day of year they made the purchase.

So, we actually want to know what day it was when the latest new customer made his first purchase.

Tuple is (Day of year, CustomerId)

let bids = [(1,1); (1,2); (1,3); (2,1); (2,2); (2,3); (3,1); (3,2); (3,66); (4,1); (4,2); (4,5); (4,5)]// from the reverse, find the first day when a customer appear that are not below in the listlet GetMaxDateOfNewCustomer (theBids : List<int * int>) = let rec custFind (custlist : List<int * int>) = let rest = custlist.Tail |> List.filter(fun x -> (snd x).Equals(snd custlist.Head)) match rest with | [] -> custlist.Head | head :: tail -> custFind custlist.Tail // remove the duplicates then sort it as a list let reverse = theBids |> Set.ofList |> Set.toList |> List.sortBy(fun x -> -(fst x)) custFind reverse let res = GetMaxDateOfNewCustomer bids System.Console.WriteLine(res)