I recently did a C# kata where I had to write a function that merged two arrays of strings together, ensuring that the resulting array only contained distinct strings. It was straightforward to write in C#, but I kept thinking I could do it easier in F#. I was right. My code is below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System; | |
let MergeLists (list1, list2) = | |
Seq.concat [ list1; list2 ] | |
|> Seq.distinct | |
[<EntryPoint>] | |
let main args = | |
// Given two lists of strings, merge the lists eliminating | |
// any duplicate entries. | |
let names1 = [ "Fred"; "Gilligan"; "Skipper" ] | |
let names2 = [ "Ginger"; "Fred"; "Maryann" ] | |
let uniqueList = MergeLists (names1, names2) | |
uniqueList | |
|> Seq.iter (fun x -> printfn "%s" x) | |
0 |