15

How can I write function which simulates while loop? It should takes 2 arguments: condition and expression to execute.

I tried the following:

val whileLoop: (Boolean,Any)=>Unit = (condition:Boolean, expression:Any) => {
 expression
 if(condition) whileLoop(condition,expression)
 () }    

But it seems it doesn't work, e.g. i have array:

val arr = Array[Int](-2,5,-5,9,-3,10,3,4,1,2,0,-20)    

Also I have variable i:

var i = 0

I want to print all elements of arr. I can do that with the following code:

while(i<arr.length) { println(tab(i)); i+=1 }

I would like to do the same using my whileLoop function. But I can't write function which takes reference to variable and modify that. I could pass that using array with only one element, e.g.

val nr = Array(0)

and function:

val printArray: Array[Int]=>Unit = (n:Array[Int]) => {
 println(arr(n(0)))
 n(0)+=1
 ()
}

and then using in my whileLoop:

whileLoop(nr(0)<arr.length, printArray)

After using above codes I get StackOverflowError and nr(0) is equals zero. Also following function:

val printArray: Array[Int]=>Unit = (n:Array[Int]) => {
 println(arr(nr(0)))
 nr(0)+=1
 ()
}

gives the same result.

How can i write correct function whileLoop and use that to print all arr elements?

Thanks in advance for advices.

2 Answers 2

29

The main problem with your implementation is that the condition and the expression are evaluated only once, when you first call whileLoop. In the recursive call, you just pass a value, not an expression.

You can solve this by using by-name arguments:

def whileLoop(cond : =>Boolean, block : =>Unit) : Unit =
  if(cond) {
    block
    whileLoop(cond, block)
  }

As an example:

scala> val a = Array(1, 2, 3)
scala> var i = 0
scala> whileLoop(i < a.length, { println(i); i += 1 })
1
2
3

Note that the variables a and i are correctly referenced. Internally, the Scala compiler built a function for both the condition and the expression (block), and these functions maintain a reference to their environment.

Also note that for more syntactic sugar awesomeness, you can define whileLoop as a currified function:

def whileLoop(cond : =>Boolean)(block : =>Unit) : Unit =
  if(cond) {
    block
    whileLoop(cond)(block)
  }

This allows you to call it just like an actual while loop:

whileLoop(i < a.length) {
  println(a(i))
  i += 1
}
Sign up to request clarification or add additional context in comments.

Comments

2

This is what i came up with: first of all, your function needs these 4 arguments:

- array which is yet to be processed
- predicate that tells the function when to stop
- function that takes the array to be processed and current state and produces a new state
- and state that is being propagated through the recurion:

i think the code is pretty self explanatory:

def whileFunc[A,B](over: Array[A], predicate: Array[A] => Boolean, apply: (Array[A],B) => B, state: B):B = {
   val doIterate = predicate(over)
   if(doIterate) whileFunc(over.tail, predicate, apply, apply(over,state)) else state
}

this could be made a lot nicer but i tried to keep it as simple as possible. To count all the elements in array, you would call it like so:

scala>     whileFunc(Array(1,2,3), (a:Array[Int]) => !a.isEmpty,(a:Array[Int],s: Int) => s + a.head, 0)
res5: Int = 6

to print each of the elements:

whileFunc[Int, Unit](Array(1,2,3), (a:Array[Int]) => !a.isEmpty,(a:Array[Int],s: Unit) => print(a.head), Unit)
123

By the way, if you are interested in this kind of stuff, i would recommend u buying Functional programming in Scala, there are two chapters which make you implement functions like this. It's a lot of fun.

1 Comment

Thanks also, it's interesting alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.