gpt4 book ai didi

F# 缩短树上的模式匹配

转载 作者:行者123 更新时间:2023-12-01 00:23:41 25 4
gpt4 key购买 nike

不确定这是否是这个问题的正确位置,但我有一个函数,我很确定可以简化它,但我不确定如何简化。

let rec probOK = function
| Branch(ds, p, Leaf l1, Leaf l2) when p <= 1.0 && p >= 0.0 -> true
| Branch(ds, p, b1, b2) when p <= 1.0 && p >= 0.0 -> probOK b1 && probOK b2
| Branch(ds, p , b1, Leaf l1) when p <= 1.0 && p >= 0.0 -> probOK b1
| Branch(ds, p , Leaf l2, b2) when p <= 1.0 && p >= 0.0 -> probOK b2
| _ -> false

任务是定义一个接受 probability tree 的函数。 (见下文)并检查是否满足每个概率 p0 <= p <= 1 .一个 probability tree有类型
type ProbTree = | Branch of string * float * ProbTree * ProbTree
| Leaf of string
probability tree是什么意思是表示连续过程的样本空间的树,其中过程中每个阶段的结果要么成功要么失败。
probability tree 的示例,掷出六面骰子的概率是 >22/3 , 概率是 <= 21/3等等:

Probability tree

在我的示例中,我正在处理的概率树是:
let test = Branch(">2",0.67, Branch(">3",0.5, Leaf "A", Leaf "B")
, Branch(">3",0.5, Leaf "C", Leaf "D"))

这将返回 true,因为所有概率 p在 0 和 1 之间。

现在,我定义的函数可以工作,但我觉得可以简化模式匹配,也许通过做一些类似于 ([],Leaf _ )-> true 的事情,但我无法完全弄清楚。

任何提示?

EDIT1:一个缩短的建议(现在有更少的空格):
let rec probOK = function
| Branch(ds, p, b1, b2) when p <= 1.0 && p >= 0.0 -> probOK b1 && probOK b2
| Leaf _ -> true
| _ -> false

最佳答案

您可以通过将树节点的遍历与对它们的操作分开来简化代码。这是一个检查节点是否有效的函数:

let nodeProbOk = function
| Branch(_, p, _, _)-> p <= 1.0 && p >= 0.0
| Leaf _ -> true

这是一个测试所有节点是否满足谓词的函数:
let rec forAllNodes pred = function
| Branch(_, _, a, b) as branch -> pred branch && forAllNodes pred a && forAllNodes pred b
| Leaf _ as leaf -> pred leaf

这就是您将它们一起使用的方式:
test |> forAllNodes nodeProbOk

这种方式的好处是你有两个比较简单的函数,可以复用 forAllNodes用于验证以外的目的。这限制了您需要在代码中使用递归的位置数量,并且应该使事情更容易推理。

关于F# 缩短树上的模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com