RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Beginning F#: Card Tricks

Discover the power of F# by writing a complete card-shuffling program in only 92 lines of code.

hen attempting to learn a new computer language, it's difficult to find a problem that's both interesting enough to bother working with and also simple enough to let you concentrate on language rather than logic. Fortunately, it turns out you can learn a lot about F# with a deck of playing cards. This article shows how to create a simple F# application that shuffles a deck and displays the cards in the console window. Along the way you'll explore:

  • Discriminated unions
  • Tuples
  • Lists and list sequence expressions
  • Functions and recursion
  • Pattern matching and functional polymorphism
What You Need
The minimum requirement for following along with the code is this article is Microsoft F# Interactive, delivered as part of the Microsoft F# download package available on MSDN. F# Interactive runs in a console window and compiles and runs F# commands interactively. However, the recommended setup is Visual Studio 2008 (or later) with Microsoft F# installed. If you don't have the full version of Visual Studio 2008, you can install the free Visual Studio 2008 Shell (Integrated Mode). After installing the Visual Studio Shell, install Microsoft F#.

Getting Started
Visual Studio gives you the option to create F# applications and class libraries, and has an integrated window running F# Interactive. F# will eventually be available as a standalone Visual Studio Express Edition product.

If you're using Visual Studio, create a new F# Application project named DevX.Cards.PartI. Click View → Other Windows → F# Interactive to display the integrated F# Interactive window.

If you aren't using Visual Studio then click Start → All Programs → Microsoft F# version → F# Interactive (console) to launch F# Interactive. Be sure to append two semicolons (;;) to the end of every expression you enter in F# Interactive. Just a warning: Some of the code snippets in this article include the semicolons; others do not.

Discriminated Unions and Enumerations
There are three types of playing cards in a standard 54-card deck: rank cards, face cards, and jokers. Rank cards and face cards have a suit: diamonds, clubs, hearts, or spades. Rank cards have a rank from two to ten, and face cards have a face: jack, queen, king, or ace. Jokers are...well, jokers.

The F# discriminated union construct is a great way to model the attributes of a playing card. For example, a suit is either diamonds, clubs, hearts, or spades, while F# discriminated unions are a type a that can be one of x, y, or z:

   type Rank =
   | Two
   | Three
   | Four
   | Five
   | Six
   | Seven
   | Eight
   | Nine
   | Ten
   type Face =
   | Jack
   | Queen
   | King
   | Ace
   type Suit =
   | Diamonds
   | Clubs
   | Hearts
   | Spades
You can use F# Interactive to explore how to create and compare discriminated unions.

Author's Note: F# Interactive is a command-line application in which you enter the code shown at the caret (>) prompt, and F# responds on the following line or lines—those that don't start with a caret. F# Interactive responds with output in the form:

   val expression-name : expression-type-and-result
If your expression doesn't have a name, then F# Interactive gives it the generic name it.

   val it : expression-type-and-result
Type these examples into F# interactive:

   > let suit1 = Diamonds;;
   val suit1 : Suit
   > let suit2 = Spades;;
   val suit2 : Suit
   > suit1 > suit2;;
   val it : bool = false
   > suit1 = suit2;;
   val it : bool = false
   > suit1 < suit2;;
   val it : bool = true
The combination of a Rank and a Suit creates a rank card, and the combination of a Face and a Suit creates a face card. F# provides a first-class construct for modeling combinations of values: tuples.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date