F#遍历树以进行特定设置

时间:2016-10-26 05:51:29

标签: f# tree

所以我坐在这个问题上,似乎无法以明智的方式解决这个问题。我在这里想要实现的是,我有一个函数descriptionOf os t,它带有Outcome listosTreet,并使用辅助函数Description返回descriptionOf'。这很有效,并用结果列表提供它:let outcomeTest = [F;S]和树: let testTree = Branch(">2",0.67, Branch(">3",0.5, Leaf "A", Leaf "B"),Branch(">3",2.5, Leaf "C", Leaf "D"))给了我以下结果:

([(F, ">2"); (S, ">3")], 0.825, "C")

现在,正如你所看到的,我已经开始制作allDescriptions,它应该取Tree并返回Set<Description>,但我不能因为爱我而弄清楚如何制作此套装。我一直在摆弄重用descriptionOf的想法,但问题就在于我看到的问题,就是我没有os给它,只有tree t。我想通过使Set<Description>上的testTree成为这样的结果来预期结果: set: [([(S,">2");(S,">3")], 0.335, "A"); ([(S,">2");(F,">3")], 0.335, "B"); ([(F,">2");(S,">3")], 0.165, "C"); ([(F,">2");(F,">3")], 0.165, "D")]

我希望我的问题有道理!任何提示都非常赞赏。

type Outcome  = | S | F
type Sample   = Outcome list    
type Tree = | Branch of string * float * Tree * Tree
            | Leaf of string
type Description = ((Sample * string) list * float * string)  

let rec descriptionOf' = function 
| (os, Branch(ds, p, tl, tr), (dsl, dp, s)) when List.length os > 1 && os.Head = F -> descriptionOf' (os.Tail, tr, (dsl @ [(os.Head, ds)], dp * (1.0 - p), ""))
| (os, Branch(ds, p, tl, tr), (dsl, dp, s)) when List.length os > 1 && os.Head = S -> descriptionOf' (os.Tail, tl, (dsl @ [(os.Head, ds)], dp * (p), ""))
| (os, Branch(ds, p, Leaf l1, Leaf l2), (dsl, dp, s)) when List.length os = 1 && os.Head = F -> (dsl @ [(os.Head, ds)], dp * (1.0 - p), l2)
| (os, Branch(ds, p, Leaf l1, Leaf l2), (dsl, dp, s)) when List.length os = 1 && os.Head = S -> (dsl @ [(os.Head, ds)], dp * (p), l1)
| _ -> failwith "Not a correct sample"

let descriptionOf os t = 
if isSample(os, t) = false then failwith "Not a correct sample" else
    descriptionOf'(os, t, ([], 1.0, ""))

let allDescriptions = Set.empty.Add() // What should this do?

0 个答案:

没有答案