Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by Reed Adams
// *reset*
open System
type Node =
| Branch of Node * Node
| Leaf
let rec GenNode depth =
if depth = 0
then Leaf
else
let d = depth - 1
Branch(GenNode d, GenNode d)
let inline GenTree depth = GenNode depth
let rec GetCheckSum = function
| Leaf -> 1
| Branch(l, r) -> 1 + (GetCheckSum l) + (GetCheckSum r)
[<EntryPoint>]
let main argv =
let depth = if argv.Length > 0
then Int32.Parse(argv.[0])
else 20
let min_depth = 4
let max_depth = Math.Max(min_depth + 2, depth)
let stretch_depth = max_depth + 1
GenTree stretch_depth
|> GetCheckSum
|> printfn "stretch tree of depth %i\t check: %i" stretch_depth
let long_lived_tree = GenTree max_depth
for cur_depth in min_depth ..2 ..max_depth do
let iterations = 1 <<< (max_depth - cur_depth + min_depth)
let mutable check = 0
for i in 1 ..iterations do
check <- check + GetCheckSum (GenTree cur_depth)
printfn "%i\t trees of depth %i\t check: %i" iterations cur_depth check
long_lived_tree
|> GetCheckSum
|> printfn "long lived tree of depth %i\t check: %i" max_depth
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.