Phillip Trelford's Array

POKE 36879,255

Fog Index

The Gunning Fog Index is an estimate of readability based on:

  • the number words per sentences
  • the number of complex words

The index estimates the years of formal education needed to understand the text on a first reading. Texts for a wide audience generally need a fog index less than 12.

If you have Silverlight 4 installed you, paste your text below to get a score:

The Gunning Fog Index formula along with some samples can be found on Wikipedia. The paste window gives results that are quite close to the values given in the samples, but this is an approximation.

The paste box was written in F# using the freely available Visual Studio 2010 Shell. If you are interested in the implementation some code snippets follow.

Compute approximate fog index:

let toFogIndex text =
    let sentences = getSentences text
    let words = sentences |> Array.collect getWords
    let complexWords =
        |> Array.filter (fun word -> word.Length>3)
        |> removeSuffixes            
        |> Array.filter (fun word -> countSyllables word >= 3)
    0.4 * ((float words.Length/float sentences.Length) + 
           (100.0 * float complexWords.Length/float words.Length))


React to text pasted into window; computing new Fog index in the background then updating the display on the UI thread:

do  pasteText.TextChanged         
    |> (fun _ -> pasteText.Text)
    |> Observable.onThreadPool
    |> toFogIndex
    |> Observable.onDispatcher
    |> Observable.subscribe (fun index -> 
        label.Text <- sprintf "Fog Index %0.2f" index
    |> remember


References: (5.71 kb)

BDD with ``TickSpec`` talk

The podcast for “BDD with TickSpec and Concurrency with Agents” from the F#unctional Londoners Meetup is now available, many thanks to Skills Matter:

Podcast content:

  • a quick look at F# in MonoDevelop
  • BDD with TickSpec with examples in Visual Studio using NUnit, MbUnit & Silverlight
  • Tomas Petricek on Concurrency with Agents including Chat and Twitter samples

This talk coincided with the November 2010 stable release of TickSpec, which supports both .Net 2.0+ and Silverlight 4. Plus examples for all major .Net testing frameworks.

All TickSpec examples presented are on CodePlex:

Thanks to everyone who made it a really fun evening!


Picture taken at the Slaughtered Lamb pub courtesy of Ankur Gurha


Thanks to a recent article by Cameron Taggart it is now possible to Create F# Silverlight Apps from Visual Studio 2010 Shell. This means that you now don’t need to use a C# project to build the XAP file; which previously outside of VS2010 Pro meant resorting to building the XAP with Visual Web Developer 2010 Express and F# libraries in the VS2010 shell. The only issue I’ve found with this solution is that it is not possible to directly add new files to the F# application project, but you can easily add existing files.

From his example I've created a small reference Silverlight solution with a Tic-tac-toe theme:

  • TicTacToe: the Silverlight application
  • Game: a Silverlight library with some game logic
  • TicTacToe.Test: a Silverlight Unit Test project for testing game logic

If you have Silverlight 4 installed you should see a Tic-tac-toe board that you can mark:

The Silverlight Unit Test project contains a small Behavioural Driven Development (BDD) example to test winning positions using TickSpec:

Feature: Winning positions

Scenario: Winning positions
    Given a board layout:
        | 1   | 2   | 3   |
        | <O> | <O> | <X> |
        | <O> |     |     |
        | <X> |     | <X> |
    When a player marks <X> at <row> <col>
    Then <X> wins
    | row    | col    | 
    | middle | right  |
    | middle | middle |
    | bottom | middle |

    | X | O |
    | X | O |
    | O | X |


The example above shows that TickSpec will execute all combinations for multiple examples blocks. This makes it possible to also run the tests with X’s swapped for O’s:


The steps in the scenario are mapped to attributed F# tick methods:

let [<Given>] ``a board layout:`` (table:Table) =
    table.Rows |> Seq.iteri (fun y row -> 
        row |> Seq.iteri (fun x value -> board.[x,y] <- parseMark value)
let [<When>] ``a player marks (X|O) at (top|middle|bottom) (left|middle|right)``
        (mark:string,Row row,Col col) =
    board.[col,row] <- parseMark mark

let [<Then>] ``(X|O) wins`` (mark:string) =
    Game.mark <- parseMark mark |> Option.get
    let line = winningLine()

With the column and row positions parsed with F# Active Patterns:

let (|Col|) = function 
    | "left" -> 0 | "middle" -> 1 | "right" -> 2
    | s -> invalidCast s

let (|Row|) = function 
    | "top" -> 0 | "middle" -> 1 | "bottom" -> 2 
    | s -> invalidCast s


If you are interested in learning more about BDD & TickSpec there is a free F#unctional Londoners Meetup Group evening event on Wed 24th Nov 2010 at Skills Matter:

Teaser for Tomas Petricek’s talk on his Agent’s:


Source code: (334.65 kb)