Scala 集合函数

发布于 2024-11-28 13:54:44 字数 1104 浏览 3 评论 0原文

在斯坦福 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?

abc 的解决方案相当简单:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紫﹏色ふ单纯 2024-12-05 13:54:44

我认为如果不迭代所有整数就不可能实现。对于伪证明,请查看所需的类型:

def subset: (a: Set, b: Set): Boolean

不知何故,当我们需要处理的只是集合 (a, a, >b) 类型为 Int =>;布尔值,和整数相等(Int, Int) =>布尔值。从这些原语中,获取 Boolean 值的唯一方法是从 Int 值开始。由于我们手中没有任何特定的 Int,唯一的选择就是迭代所有它们。

如果我们有一个神奇的神谕,isEmpty: Set =>;布尔,故事会有所不同。

最后一个选项是将“false”编码为空集,将“true”编码为其他内容,从而将所需类型更改为:

def subset: (a: Set, b: Set): Set

使用此编码,逻辑“或”对应于集合并集运算,但我不知道逻辑上的“与”或“非”可以轻易定义。

I don't think it's possible without iterating through all the integers. For a pseudo-proof, look at the desired type:

def subset: (a: Set, b: Set): Boolean

Somehow, we've got to produce a Boolean when all we have to work with are sets (a, b) of type Int => Boolean, and integer equality (Int, Int) => Boolean. From these primitives, the only way to get a Boolean value is to start with Int values. Since we don't have any specific Int'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:

def subset: (a: Set, b: Set): Set

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.

后eg是否自 2024-12-05 13:54:44

我们有

Set A = 
    Returns the intersection of the two given sets,
    the set of all elements that are both in `s` and `t`.

Set B = 
    Returns the subset of `s` for which `p` holds.

不是集合 A 等于集合 B

def filter(s: Set, p: Int => Boolean): Set = intersect(s, p)

We have

Set A = 
    Returns the intersection of the two given sets,
    the set of all elements that are both in `s` and `t`.

Set B = 
    Returns the subset of `s` for which `p` holds.

Isn't Set A is equivalent to Set B

def filter(s: Set, p: Int => Boolean): Set = intersect(s, p)
酒绊 2024-12-05 13:54:44

我同意 Kipton Barros 的观点,您必须检查 Ints 的所有值,因为您想证明对于所有 x,a(x) 意味着 b(x)。

关于它的优化,我可能会写:

  def subset(a: Set, b: Set) = Int.MinValue to Int.MaxValue exists(i => !a(i) || b(i))

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:

  def subset(a: Set, b: Set) = Int.MinValue to Int.MaxValue exists(i => !a(i) || b(i))

since !a(i) || b(i) is equivalent to a(i) implies b(i)

不必你懂 2024-12-05 13:54:44

稍后在 Coursera 练习中介绍了有界集,然后介绍了 forall() 和exists() 作为边界上的通用量词和存在量词。 subset() 不在练习中,但与 forall 类似。这是我的子集()版本:

// subset(s,p) tests if p is a subset of p returning true or false
def subset(s: Set, p: Set): Boolean = {
  def iter(a: Int): Boolean = {
    if (a > bound) { true
    } else if (contains(p, a)) {
        if (contains(s, a)) iter(a + 1) else false
    } else iter(a+1)
  }
  iter(-bound)
}

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():

// subset(s,p) tests if p is a subset of p returning true or false
def subset(s: Set, p: Set): Boolean = {
  def iter(a: Int): Boolean = {
    if (a > bound) { true
    } else if (contains(p, a)) {
        if (contains(s, a)) iter(a + 1) else false
    } else iter(a+1)
  }
  iter(-bound)
}
美人骨 2024-12-05 13:54:44

这是使用 contains 函数的另一个版本:

def union(s: Set, t: Set): Set = x => contains(s,x) || contains(t,x)

def intersect(s: Set, t: Set): Set = x => contains(s,x) && contains(t,x)

def diff(s: Set, t: Set): Set = x => contains(s,x) && !contains(t,x)

def filter(s: Set, p: Int => Boolean): Set = x =>  contains(s, x) && p(x)

Here is another version of it using contains function:

def union(s: Set, t: Set): Set = x => contains(s,x) || contains(t,x)

def intersect(s: Set, t: Set): Set = x => contains(s,x) && contains(t,x)

def diff(s: Set, t: Set): Set = x => contains(s,x) && !contains(t,x)

def filter(s: Set, p: Int => Boolean): Set = x =>  contains(s, x) && p(x)
只怪假的太真实 2024-12-05 13:54:44

如果有两个集合 A 和 B,则 A 与 B 相交是 A 和 B 的子集。数学证明:A ∩ B ⊆ A 和 A ∩ B ⊆ B。函数可以这样写:

def filter(s: Set, p: Int => Boolean): Set = x => s(x) && p(x)

或者

def intersect(s: Set, t: Set): Set = x => s(x) && t(x)
def filter(s: Set, p: Int => Boolean): Set = intersect(s,p)

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:

def filter(s: Set, p: Int => Boolean): Set = x => s(x) && p(x)

Or

def intersect(s: Set, t: Set): Set = x => s(x) && t(x)
def filter(s: Set, p: Int => Boolean): Set = intersect(s,p)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文