F# - Expected to be option but has a type -
i trying build binary tree in f# when tried test code, met problem above.
here code:
type treenode<'a> = { key: int; val: 'a } type tree<'a> = { lt: tree<'a> option; treenode: treenode<'a>; rt: tree<'a> option; } //insert node according binary tree operation let rec insert (node: treenode<'a>) (tree: tree<'a> option) = match tree | none -> {lt = none; rt = none; treenode = node } | t when node.key < t.treenode.key -> insert node t.lt | t when node.key > t.treenode.key -> insert node t.rt let t = seq { in 1 .. 10 -> { key = i; val = } }|> seq.fold (fun -> insert a) none
your insert function takes option<tree<'t>> returns tree<'t>. when performing fold, need keep state of same type - if want use none represent empty tree, state needs optional type.
the way fix wrap result of insert in some:
let tree = seq { in 1 .. 10 -> { key = i; val = } } |> seq.fold (fun -> some(insert a)) none
Comments
Post a Comment