Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a function but also make sure I implement another one. Is it possible?

I'm following the coursera scala course so please care that you don't just give me already baked code as this will violate the honour code. I'm not looking for code but rather a logic to an algorithm and how I might implement something that I have in mind.

First, here is the problem:

Using 'forall', implement a function 'exists' which tests whether a set contains at least one element for which the given predicate is true. Note that the functions 'forall' and 'exists' behave like the universal and existential quantifiers of first-order logic.

Pretty much from what I understand is :

Create 'exists'. Make sure forall it's implemented somewhere there.

I have created the forall:

  def forall(s: Set, p: Int => Boolean): Boolean = {
    def iter(a: Int): Boolean = {
      if (a > bound) true
      else if (contains(s,a)) if(!p(a)) false else iter(a+1) 
      else iter(a+1)
    }
    iter(-bound)
  }

However how can this function help me in any way to create the exists? I mean what I have in mind for exists is exactly the same function but instead of iterating when the condition is met , I only have to return true because one case has been found. Theoretically I don't need to care about other cases so iterating is only needed at the point where the second 'if' returns true ( which means that one of the numbers from the first set cannot cannot be applied to the second function). Let me illustrate this a bit better:

 def exists(s: Set, p: Int => Boolean): Boolean = {
   def iter(a: Int): Boolean = {
     if (a > bound) true
     else if (contains(s,a)) if(!p(a)) iter(a+1) else true 
     else iter(a+1)
   }
   iter(-bound)
 }

My question is weather what I'm thinking is correct and if it is then how would I implement forall as the function is quite different from what is needed to complete this. If I'm missing the point could you point me in a direction to how I should tackle this?

Note Please do not give me baked code

like image 757
Bula Avatar asked Nov 15 '25 13:11

Bula


1 Answers

A predicate p is true for some element(s) of a set, if it is not false for all elements of the set.

Given your question, it appears as Set is an iterable collection of Int, since the predicate has type Int => Boolean.

You can create instances of this predicate type by creating a lambda expression e.g.

val gt: Int => Boolean = x => x > 10

which you can then call with given values

gt(4)  //false
gt(11) //true

The key to this problem is therefore negating a given predicate i.e. you need to create a function with the following signature:

def negate(p: Int => Boolean): Int => Boolean

once you have written that you can implement exists in terms of your foreach function directly from the definition.

like image 127
Lee Avatar answered Nov 18 '25 03:11

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!