I'm just getting into functional programming and i'm in the "try out some non-trivial examples and ask others if I'm doing it wrong" phase. I'm following Don Syme's F# Tutorial and have decided to take a stab at the blackjack exercise at the end of Part II with a twist: he suggests treating Ace as 11 for simplicity's sake, but I decided to ignore that recommendation.
The way I'm handling it is by giving each card rank a list of possible values and building up a list of possible hand values recursively thus:
let cardValues (Card(rank, _)) =
match rank with
| Ace -> [1; 11]
| King | Queen | Jack -> [10]
| Value(value) -> [value]
let rec handValues = function
| [] -> [0]
| card::cards ->
[
for handValue in handValues cards do
for cardValue in cardValues card do
yield handValue + cardValue
]
The handValues
function is so similar in structure to a fold that I can't shake the feeling there's already some high order function I can use to accomplish this. Is there something I'm missing or is this pretty much the right direction?