F# Lingo Guesser
I was watching Lingo and figured it would be good problem to experiment with F# on. For the non game show watchers out there its basically the word game version of mastermind with letters instead of colored pegs. F# pattern matching and discriminate unions made it elegant to write up. For instance here is all the code to get possible solutions given the set of known clues.
type Clue =
| FullMatch of int * char //Correct letter at correct position
| PartialMatch of int * char //Correct letter at wrong position
| Excluded of char * int //No more than n instances of the letter
let GetPossibleSolutions clues =
let IsValidWord (word : string) =
clues |> Seq.forall (function
| FullMatch(i, c) -> word.[i] = c
| PartialMatch(i, c) -> word.[i] <> c && word |> Seq.exists ((=) c)
| Excluded(c, i) -> word |> count_of c = i
)
Words.words |> Array.filter IsValidWord
With the discriminate union and pattern matching its pretty understandable even without a great deal of F# knowledge. Basically it just filters out any words that doesn’t fit any of the clues. Attached is the source code for a simple console app in F# that gives you guesses to try and refines them based off the resulting clues.