Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:08:08+00:00 2026-06-14T04:08:08+00:00

How can I write function which simulates while loop? It should takes 2 arguments:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:08:10+00:00Added an answer on June 14, 2026 at 4:08 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes multiple arguments, which can come either
How can I write (if it's possible at all...) a function which takes an
I'm trying to make it so I can write a function which defines a
How can I write a function which will return pi (π) to a given
I want to write a function which can validate a given value (passed as
Let's consider the following scenario: a function which can generate code colors from white
How can I write a function that accepts a variable number of arguments? Is
I would like to write a function which takes an fstream , processes it,
hello everyone I'm trying to write function which can unfold int in the list
Does anyone know how I can write a Javacript function which returns true if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.