Skip to content

Instantly share code, notes, and snippets.

@NickHeiner
Created April 20, 2012 17:38
Show Gist options
  • Save NickHeiner/2430532 to your computer and use it in GitHub Desktop.
Save NickHeiner/2430532 to your computer and use it in GitHub Desktop.
(* Nick Heiner *)
let data = Array.toList ("LLLQLLQLQLLLQLELLLLLQLLQQLLLQE".ToCharArray())
let transitionCounts =
let rec helper soFar = function
| [] -> soFar
| [hd] -> soFar
| first::((next::_) as tl) ->
let key = (first, next)
let prevCount = if Map.containsKey key soFar then Map.find key soFar else 0
helper (Map.add (first, next) (prevCount + 1) soFar) tl
helper Map.empty data
let totalTransitions = Map.fold (fun acc key count -> acc + count) 0 transitionCounts
Map.iter (fun (first, next) count -> Printf.printf "%A => %A: %f\n" first next ((float) count / (float)totalTransitions)) transitionCounts
(*
'E' => 'L': 0.034483
'L' => 'E': 0.034483
'L' => 'L': 0.413793
'L' => 'Q': 0.241379
'Q' => 'E': 0.034483
'Q' => 'L': 0.206897
'Q' => 'Q': 0.034483
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment