Skip to content

Instantly share code, notes, and snippets.

@Gab-km Gab-km/gifts.fs
Created Dec 15, 2015

Embed
What would you like to do?
某所のアレをやってみた
type Family = Blair | Smith | Kelly | Tavares | Chen | Singh
type Person = { First: string; Last: Family }
let blair's first = { First = first; Last = Blair }
let smith's first = { First = first; Last = Smith }
let kelly's first = { First = first; Last = Kelly }
let tavares' first = { First = first; Last = Tavares }
let chen's first = { First = first; Last = Chen }
let singh's first = { First = first; Last = Singh }
let gift person giftee =
if person.Last = giftee.Last then None
else Some(person.First, giftee.First)
let giftAll ns =
let rec giftAllrec ns result =
match ns with
| name::rest ->
let newResult =
List.map (gift name) rest
|> List.append result
giftAllrec rest newResult
| _ -> result
giftAllrec ns []
let names = [
blair's "Toby"
smith's "Ian"
kelly's "Martin"
kelly's "Kate"
smith's "John"
tavares' "Bruno"
chen's "Vicky"
kelly's "Sarah"
kelly's "Mark"
singh's "Ajay"
]
giftAll names
|> List.choose id
|> List.iter (fun (name, giftee) ->
printfn "%s -> %s" name giftee
printfn "%s -> %s" giftee name
)
(*
Result
-------------
Toby -> Ian
Ian -> Toby
Toby -> Martin
Martin -> Toby
Toby -> Kate
Kate -> Toby
Toby -> John
John -> Toby
Toby -> Bruno
Bruno -> Toby
Toby -> Vicky
Vicky -> Toby
Toby -> Sarah
Sarah -> Toby
Toby -> Mark
Mark -> Toby
Toby -> Ajay
Ajay -> Toby
Ian -> Martin
Martin -> Ian
Ian -> Kate
Kate -> Ian
Ian -> Bruno
Bruno -> Ian
Ian -> Vicky
Vicky -> Ian
Ian -> Sarah
Sarah -> Ian
Ian -> Mark
Mark -> Ian
Ian -> Ajay
Ajay -> Ian
Martin -> John
John -> Martin
Martin -> Bruno
Bruno -> Martin
Martin -> Vicky
Vicky -> Martin
Martin -> Ajay
Ajay -> Martin
Kate -> John
John -> Kate
Kate -> Bruno
Bruno -> Kate
Kate -> Vicky
Vicky -> Kate
Kate -> Ajay
Ajay -> Kate
John -> Bruno
Bruno -> John
John -> Vicky
Vicky -> John
John -> Sarah
Sarah -> John
John -> Mark
Mark -> John
John -> Ajay
Ajay -> John
Bruno -> Vicky
Vicky -> Bruno
Bruno -> Sarah
Sarah -> Bruno
Bruno -> Mark
Mark -> Bruno
Bruno -> Ajay
Ajay -> Bruno
Vicky -> Sarah
Sarah -> Vicky
Vicky -> Mark
Mark -> Vicky
Vicky -> Ajay
Ajay -> Vicky
Sarah -> Ajay
Ajay -> Sarah
Mark -> Ajay
Ajay -> Mark
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.