Skip to content

Instantly share code, notes, and snippets.

@thedgbrt
Last active February 3, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedgbrt/9e76bdfa785d5a26f4ade9037256bde1 to your computer and use it in GitHub Desktop.
Save thedgbrt/9e76bdfa785d5a26f4ade9037256bde1 to your computer and use it in GitHub Desktop.
(* It's code I wrote while solving a problem in a Functional Programming course in December 2016 *)
(* Coming from OOP, I love how elegant and readable it is *)
fun card_value ( card : card ) =
case card of
( _, Num i ) => i
| ( _, Ace ) => 11
| ( _, _ ) => 10
fun sum_cards ( cs : card list ) =
let
fun sum ( cs, acc ) =
case cs of
[] => acc
| c :: cs' => sum ( cs', acc + card_value c )
in
sum ( cs, 0 )
end
val test = sum_cards [(Clubs, Num 2),(Clubs, Num 2)] = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment