所以我坐在这个问题上,似乎无法以明智的方式解决这个问题。我在这里想要实现的是,我有一个函数descriptionOf os t
,它带有Outcome list
,os
和Tree
,t
,并使用辅助函数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?