Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write function simulating while loop in Scala

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.

like image 842
Paul Avatar asked Nov 11 '12 13:11

Paul


People also ask

Is Scala a functional language?

Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and it supports currying.

Should you use loops in Scala?

If you heard about loops in Scala, try to forget them. Try to do without them. Instead of asking “how can I loop through this”, ask “how can I transform this”. Take this one principle to heart and you'll be ahead of many Scala programmers already.


2 Answers

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
}
like image 52
Philippe Avatar answered Nov 09 '22 22:11

Philippe


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.

like image 39
Arg Avatar answered Nov 09 '22 23:11

Arg