Skip to content

Instantly share code, notes, and snippets.

@chribben
Created May 31, 2012 22:39
Show Gist options
  • Save chribben/2846874 to your computer and use it in GitHub Desktop.
Save chribben/2846874 to your computer and use it in GitHub Desktop.
FuncProgSTHLM challenge
//Challenge https://groups.google.com/group/functional-programming-sthlm/browse_thread/thread/647165dc2d923529?hl=sv
// The record type Part
type Part = {
Heading : string;
Text : string;
IsColumnLayout : bool;
}
// Make some test data
let r = { Heading="Wide part"; Text="This is a wide part"; IsColumnLayout=false }
let g = { Heading="Column part"; Text="This is a column"; IsColumnLayout=true }
// A test list
let rnd = new System.Random()
let parts = [for x in 0..99999 -> rnd.Next(0, 2) |> fun v -> if v = 0 then r else g]
// The actual function, using pattern matching
let mapParts l =
let rec mapParts' l newl =
match l with
| p :: r :: tail when p.IsColumnLayout && r.IsColumnLayout -> let newl = (p, Some(r))::newl
mapParts' tail newl
| p :: tail -> let newl = (p, None)::newl
mapParts' tail newl
| [] -> List.rev(newl)
mapParts' l []
//Test it
parts |> mapParts |> Seq.iter (printf "%A\r\n___\r\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment