Frage

This question relates to the scala course from coursera so I want to please ask you to not give me the plain solution that I can copy-paste as this would break the coursera honor code.

This relates to the second assignment.

def Set = Int => Boolean

As it can be seen, Set is a function which returns weather or not the given int is or not part of the set. This is plain and simple so far. However the task asks me to create a union

 def union(f: Set, s: Set): Set = ???

This union should return a set that satisfies the condition of both sets. How could I do something like this:

I thought that such a thing could be done by adding the functions together however the following code:

f + s

Will not compile properly as expected

My question to is:

How would I be able to create a function from 2 other functions?

x => if x == 0 true else false //first
x => if x == 1 true else false //second

And what should equal:

x => if x==0 || x == 1 true else false

I'm not asking for a solution but rather how would I go around building something like this?

War es hilfreich?

Lösung

As I think you already understand, these Sets are functions that test whether a value meets the criteria for each Set.

The union of such a Set must also be a function that returns a Boolean (as shown by the type signature)

def union(f: Set, s: Set): Set

which (because Set is a type alias) is equivalent to:

def union(f: Int => Boolean, s: Int => Boolean): Int => Boolean

In plain English, union of two sets A and B means: "is the item in A or B".

Your task is to write a function that carries out that plain English specification.

You cannot "add" two functions together (at least, not in a way that is applicable to this question), but you can combine their results.

Andere Tipps

The Set has form of Set = Int => Boolean. Given the Int function will return true if the value is in a Set.

Well if we want to create a singleton set, we will return new function, which will compare any value passed to it, with the one passed to the function that created it.

The union of two sets, is one set plus the other. It means the element you're looking for must be either in one or the other set. But how do we get the new set, well we return a new function that does just that - checks if an element is either in one set or another.

Remember that in Scala functions can return functions, which may be evaluated later. I think that's the key.

The Set is defined as a function from Int to Boolean, "summing" two Sets won't return a Set object, the union means that one element should be either in one or in the other set but always expressed as a function.

I hope this is not too much, but given an element it should satisfy either f or s.

First of all, it's type Set =. Not def. Set is a type alias not a function definition.

Now, your question. You need a function which, when given two Int =>Boolean combines them with OR and returns a Int => Boolean.

First, how would you do this for two Boolean arguments?

def or(a: Boolean, b: Boolean) = a || b

So now we're half way there. What we have:

  • A pair of Int => Boolean functions.
  • A function that takes two Booleans and return a Boolean.

So all we need to do is apply each Set to an Int to get a Boolean and OR the result. The confusion is probably here.

The easiest way to curry a function is to do it explicitly

def union(f: Set, s: Set): Set = {
    def doUnion(x: Int) = //apply x to f and s, return OR
    doUnion
}

But we can, in Scala, so this inline by declaring an anonymous function

def union(f: Set, s: Set): Set = x => //apply x to f and s, return OR
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top