scala 作业帮助:集合之间的并集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12651079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assignment help: Union between sets
提问by Andrew
I am taking a Coursera Functional Programming in Scala class. This is the second week and I hit a wall. In the assignment we are working with Sets, but not the kind of Set we all meet in Java, for example. It is a Set that returns true if the value is in there and false otherwise. They say it's not a container, it's just a function.
我正在 Scala 课程中学习 Coursera 函数式编程。这是第二周,我碰壁了。例如,在作业中,我们使用的是 Sets,但不是我们在 Java 中遇到的那种 Set。它是一个 Set,如果值在那里则返回 true,否则返回 false。他们说它不是一个容器,它只是一个函数。
To get it clear, I need your help. I don't want you to solve my assignment, it's just an example that I want to get the idea of what I should do.
为了弄清楚,我需要你的帮助。我不想让你解决我的任务,这只是一个例子,我想知道我应该做什么。
/**
* We represent a set by its characteristic function, i.e.
* its `contains` predicate.
*/
type Set = Int => Boolean
/**
* Indicates whether a set contains a given element.
*/
def contains(s: Set, elem: Int): Boolean = s(elem)
/**
* Returns the set of the one given element.
*/
def singletonSet(elem: Int): Set = Set(elem)
/**
* Returns the union of the two given sets,
* the sets of all elements that are in either `s` or `t`.
*/
def union(s: Set, t: Set): Set = ???
This is the code. In the singletonSetI guess the way to solve it is to return the Set(elem), right?
这是代码。在singletonSet我猜解决它的方法是返回Set(elem),对吗?
If that is good, how am I supposed to make the union between the two? I am not new to programming but I can't see any way to do it. Since I shouldn't return a "set" of numbers.
如果那很好,我应该如何在两者之间建立联盟?我对编程并不陌生,但我看不到任何方法。因为我不应该返回“一组”数字。
This is what another student told me about sets: "But all a "Set" is is a function that takes an Int and returns a Boolean (Int => Boolean). Any function that takes an Int and returns a Boolean fits the type 'Set'."
这是另一个学生告诉我的关于集合的内容:“但是所有的“集合”都是一个函数,它接受一个 Int 并返回一个布尔值 (Int => Boolean)。任何接受一个 Int 并返回一个布尔值的函数都适合类型 '设置'。”
What I tried in the union function is to have something like:
我在 union 函数中尝试过的东西是这样的:
def union(s: Set, t: Set): Set = (s | t) //value | not a member of Int => Boolean
Any help would be appreciated :)
任何帮助,将不胜感激 :)
回答by huynhjl
It seems the wall you are hitting is that you are unfamiliar with defining functions in Scala. In this particular case you need to define functions of type Int => Boolean, they take an Intand return a Boolean.
您遇到的问题似乎是您不熟悉在 Scala 中定义函数。在这种特殊情况下,您需要定义类型为 的函数Int => Boolean,它们采用 anInt并返回 a Boolean。
Here are some examples of function literals of type Int => Boolean. Try them in the Scala console or the Scala IDE worksheet:
下面是一些类型为函数文字的例子Int => Boolean。在 Scala 控制台或 Scala IDE 工作表中尝试它们:
(x: Int) => true
(x: Int) => false
(x: Int) => x == 2
(x: Int) => x == 10
(x: Int) => x == 2 || x == 10
(x: Int) => x % 2 == 0
Then all you have to do for the assignment is to use the same syntax, starting with (x: Int) =>and then translate the meaning of union, intersect, ... into the right hand side of the expression.
然后,您需要为赋值做的就是使用相同的语法,从(x: Int) =>并开始,然后将 union、intersect、... 的含义翻译到表达式的右侧。
Part of learning is giving it a genuine effort. I believe you can resubmit the solution multiple times, so don't hesitate to submit and iterate if you don't get 10/10 on the first try. All you need is compiling code. Good luck!
学习的一部分是给予它真正的努力。我相信您可以多次重新提交解决方案,因此如果您在第一次尝试时没有获得 10/10,请不要犹豫提交和迭代。您所需要的只是编译代码。祝你好运!
回答by Nicolas
A possible hint is to look at the types. Look at the Settype. It is actually a type alias to a function from Intinto Boolean.
一个可能的提示是查看类型。看Set类型。它实际上是函数 from Intinto的类型别名Boolean。
Thus, when you have two sets, you actually have two functions. How can you use them to provide a function that represent the union of these Set? It must be your starting point.
因此,当你有两个集合时,你实际上有两个功能。您如何使用它们来提供表示这些 Set 并集的函数?它必须是您的起点。

