Software Testing, F#, and the Sapir-Whorf Hypothesis

The new F# programming language is interesting. One possible reason for learning a new programming language is an effect that is closely related to something called the Sapir-Whorf hypothesis (SWH). In over-simplified terms, the SWH says that speakers of different languages (English, Japanese, etc.) think differently because of differences in languages. So an extension of this idea is that programmers and testers who use C# may program and test differently than those who use LISP, and so the more programming languages you know, the more programming patterns you have available. That aside, in a recent Introduction to F# Programming class I taught, most students (and I) said they were in the class out of technical curiosity and for intellectual entertainment. One of the examples I put together for the class has a nice set of thematic F# syntax and paradigms. See the image below for a sample run.
 1 #light
 2 open System
 3 printfn "\nBegin F# deck of cards demo"
 4 let deck =
 5   [| for suit in ["Clubs"; "Diamonds"; "Hearts"; "Spades" ] do
 6     for rank in [1..13] do
 7     yield (rank,suit) |]
 9 printfn "\nThe unshuffled deck is %A \n" deck   
10 let ro = new Random(0) 
11 let shuffle (a : (int*string) array) =
12   let len = a.Length
13   for i in 0..len-1 do
14     let r = ro.Next(i, len-1)
15     let tmp = a.[r]
16     a.[r] <- a.[i]
17     a.[i] <- tmp
19 deck |> shuffle
21 let printDeck (d : (int*string) array) =
22   let n = d.Length
23   for i in 0..n-1 do
24     if i > 10 && i < 49 then printf "."
25     elif i = 49 then printf "\n"
26     else
27       printf "card[%d] = " i
28       if i < 10 then printf " "
29       let (rank,suit) = d.[i]
30       if rank < 10 then printf " "
31       printfn "%d of %s" rank suit
33 printfn "\nThe shuffled deck is: "
34 deck |> printDeck
36 printfn "\nEnd deck of cards demo"
37 System.Console.ReadLine() |> ignore
Lines 4-7 construct a mutable array of size 52, where each cell holds a tuple composed of an int (a card value from 1-13) and a string ("Clubs", etc.) Line 9 uses the %A format specifier to instruct the F# compiler to use its own judgment on how to print the array of card tuples. Lines 11-17 define a function which uses the Fisher-Yates shuffle algorithm to shuffle an array in place. Line 19 pipes the deck to the shuffle function. Lines 21-31 define a custom print function. Line 37 points out that F# is a .NET language and can access the .NET Framework. One thing I found interesting is that, more than any language I can think of, F# has many different ways to perform programming tasks. For example, Lines 29-31:
let (rank,suit) = d.[i]
if rank < 10 then printf " "
printfn "%d of %s" rank suit
can also be coded as:
match d.[i] with
| (rank,suit) ->
  if rank < 10 then printf " "
  printfn "%A of %A" rank suit
which is more F#-ish. Anyway, F# is really interesting in many ways.
This entry was posted in Software Test Automation. Bookmark the permalink.