How to resolve the algorithm Loops/Do-while step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Do-while step by step in the Scala programming language
Table of Contents
Problem Statement
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Do-while step by step in the Scala programming language
Source code in the scala programming language
{
var (x, l) = (0, List[Int]())
do {
x += 1
l :+= x // A new copy of this list with List(x) appended.
} while (x % 6 != 0)
l
}.foreach(println(_))
def loop(iter: Int, cond: (Int) => Boolean, accu: List[Int]): List[Int] = {
val succ = iter + 1
val temp = accu :+ succ
if (cond(succ)) loop(succ, cond, temp) else temp
}
println(loop(0, (_ % 6 != 0), Nil))
def loop(i: Int, cond: (Int) => Boolean): Stream[Int] = {
val succ = i + 1;
succ #:: (if (cond(succ)) loop(succ, cond) else Stream.empty)
}
loop(0, (_ % 6 != 0)).foreach(println(_))
You may also check:How to resolve the algorithm Parse an IP Address step by step in the Rust programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Forth programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm File size step by step in the Scheme programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the langur programming language