Scala 集合函数
在斯坦福 Scala 课程中,我遇到了以下作业:
练习 1 – 集合作为函数:
在本练习中,我们将把集合表示为从整数到布尔值的函数:
type Set = Int => Boolean
a)编写一个函数“set”,它接受一个 Int 参数并返回一个包含该 Int 的 Set。
b) 编写一个“包含”函数,将 Set 和 Int 作为参数,如果 Int 在 Set 中则返回 true,否则返回 false。
c) 编写函数“union”、“intersect”和“minus”,它们将两个 Set 作为参数并返回一个 Set。
d) 你能写一个函数“subset”,它接受两个 Sets 作为参数,如果第一个 Sets 是第二个 Sets 的子集,则返回 true,否则返回 false?
a、b 和 c 的解决方案相当简单:
def set(i: Int): Set = n => n == i
def contains(s: Set, i: Int) = s(i)
def union(a: Set, b: Set): Set = i => a(i) || b(i)
def intersect(a: Set, b: Set): Set = i => a(i) && b(i)
def minus(a: Set, b: Set): Set = i => a(i) && !b(i)
但是对于 d 是否有任何优雅的解决方案? 当然,严格来说,d 的答案是“是”,因为我可以写这样的内容:
def subset(a: Set, b: Set) = Int.MinValue to Int.MaxValue filter(a) forall(b)
但这可能不是正确的方式。
In Stanford Scala course I've come across the following assignment:
Exercise 1 – Sets as Functions:
In this exercise we will represent sets as functions from Ints to Booleans:
type Set = Int => Boolean
a) Write a function "set" that takes an Int parameter and returns a Set containing that Int.
b) Write a function "contains" that takes a Set and an Int as parameters and returns true if the Int is in the Set and false otherwise.
c) Write the functions "union", "intersect", and "minus" that take two Sets as parameters and return a Set.
d) Can you write a function "subset" which takes two Sets as parameters and returns true if the first is a subset of the second and false otherwise?
Solutions to the a, b and c are fairly trivial:
def set(i: Int): Set = n => n == i
def contains(s: Set, i: Int) = s(i)
def union(a: Set, b: Set): Set = i => a(i) || b(i)
def intersect(a: Set, b: Set): Set = i => a(i) && b(i)
def minus(a: Set, b: Set): Set = i => a(i) && !b(i)
But is there any elegant solution for d?
Of course, strictly speaking, the answer to d is "yes", as I can write something like:
def subset(a: Set, b: Set) = Int.MinValue to Int.MaxValue filter(a) forall(b)
but that's probably not the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为如果不迭代所有整数就不可能实现。对于伪证明,请查看所需的类型:
不知何故,当我们需要处理的只是集合 (
a
,a
,>b
) 类型为Int =>;布尔值
,和整数相等(Int, Int) =>布尔值
。从这些原语中,获取Boolean
值的唯一方法是从Int
值开始。由于我们手中没有任何特定的 Int,唯一的选择就是迭代所有它们。如果我们有一个神奇的神谕,
isEmpty: Set =>;布尔
,故事会有所不同。最后一个选项是将“false”编码为空集,将“true”编码为其他内容,从而将所需类型更改为:
使用此编码,逻辑“或”对应于集合并集运算,但我不知道逻辑上的“与”或“非”可以轻易定义。
I don't think it's possible without iterating through all the integers. For a pseudo-proof, look at the desired type:
Somehow, we've got to produce a
Boolean
when all we have to work with are sets (a
,b
) of typeInt => Boolean
, and integer equality(Int, Int) => Boolean
. From these primitives, the only way to get aBoolean
value is to start withInt
values. Since we don't have any specificInt
's in our hands, the only option is to iterate through all of them.If we had a magical oracle,
isEmpty: Set => Boolean
, the story would be different.A final option is to encode "false" as the empty set and "true" as anything else, thus changing the desired type to:
With this encoding, logical "or" corresponds to the set union operation, but I don't know that logical "and" or "not" can be defined easily.
我们有
不是集合 A 等于集合 B
We have
Isn't Set A is equivalent to Set B
我同意 Kipton Barros 的观点,您必须检查 Ints 的所有值,因为您想证明对于所有 x,a(x) 意味着 b(x)。
关于它的优化,我可能会写:
since
!a(i) || b(i)
等价于a(i) 暗示 b(i)
I agree with Kipton Barros, you would have to check all values for Ints since you want to prove that
forall x, a(x) implies b(x)
.Regarding the optimization of it, I'd probably write:
since
!a(i) || b(i)
is equivalent toa(i) implies b(i)
稍后在 Coursera 练习中介绍了有界集,然后介绍了 forall() 和exists() 作为边界上的通用量词和存在量词。 subset() 不在练习中,但与 forall 类似。这是我的子集()版本:
Later on in the Coursera exercises bounded sets are introduced and then forall() and exists() as universal and existential quantifiers over the bounds. subset() was not in the exercises but is similar to forall. Here is my version of subset():
这是使用 contains 函数的另一个版本:
Here is another version of it using contains function:
如果有两个集合 A 和 B,则 A 与 B 相交是 A 和 B 的子集。数学证明:
A ∩ B ⊆ A 和 A ∩ B ⊆ B
。函数可以这样写:或者
If there are two sets A and B, then A intersect B is a subset of A and B. Mathematically proven:
A ∩ B ⊆ A and A ∩ B ⊆ B
. Function can be written like this:Or