Here is the question.
I have this type Set:
type Set = Int => Boolean
Which i can use like this:
val belowNegFive: Set = (i) => i < -5
belowNegFive(10)
I mean to return a bool depending if the elem 10 pertains to the set of numbers below -5.
- I have this code, which returns the subset of
s
for whichp
holds.
def filter(s: Set, p: Int => Boolean): Set = (e: Int) => s(e) && p(e)
Q1: How do I know p(e) tells me that the int e satisfies the predicate p?
- I have this fc, which returns whether all bounded integers within
s
satisfyp
.
def contains(s: Set, elem: Int): Boolean = s(elem) val bound = 1000 def forall(s: Set, p: Int => Boolean): Boolean = { def iter(a: Int): Boolean = { if (a > bound) true else if (contains(s, a) && !p(a)) false else iter(a+1) } iter(-bound) }
Q2: Why do all a > bound simply satisfy the condition of the predicate by default? Or true is just a stop condition? I am not sure why this fc does not return an infinite loop. Just an infinite list of Booleans, and the last Booleans are "true, true, true ..." for all a > bound.
Q3: And i do not see in which point it uses && between resulting Booleans in order to say yes, all bounded integers within s
satisfy p
.
thank you