Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala set function

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.

like image 403
Grozz Avatar asked Aug 05 '11 23:08

Grozz


People also ask

Is Scala Set a HashSet?

As per Wikipedia, a Set is a data structure which allows you to store some values but where the values cannot be repeatable. In Scala, a HashSet is a concrete implementation of Set semantics. The HashSet will use the element's hashCode as a key to allow for fast lookup of the element's value within the HashSet.

What is difference between Set and list in Scala?

A Set is unordered and can't have duplicate items. Sequences ( Array , List , Vector , etc) are ordered and can have repeated elements. Save this answer.

Is Set ordered in Scala?

The default representation of a SortedSet is an ordered binary tree which maintains the invariant that all elements in the left subtree of a node are smaller than all elements in the right subtree. That way, a simple in order traversal can return all tree elements in increasing order. Scala's class immutable.

Is Set mutable Scala?

Some Important Points about Set in ScalaBy default set in Scala are immutable. In Scala, the immutable set is defined under Scala. collection. immutable.


1 Answers

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.

like image 109
Kipton Barros Avatar answered Nov 15 '22 23:11

Kipton Barros