GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Tuesday 11 May 2004
Chapter 3, Exercises 7-8, Thinking in Scala

Here, we finally use an import statement, because I didn't want to say Math.floor(Math.random(...)). Exercise 8 just adds the infinite loop (shown in red here) to the code for exercise 7.

Here are the corresponding THECLAPP entries:

http://theclapp.blog-city.com/read/10083.htm
http://theclapp.blog-city.com/read/10086.htm

package x;
import Math.floor;
import Math.random;
object c3x7_and_8 {
    def main ( args: Array[String] ) = {
        while (true) {
            val fr : Double = (floor(random() * 25.0)); 
            val pivot : Int = fr.asInstanceOf[Int];
            for (val i <- Iterator.range( 0, 25 ) ) {
                val r : Int = (floor(random() * 25)).asInstanceOf[Int];
                Console.println( r +
                     ( if (r < pivot)      " < ";
                       else if (r > pivot) " > ";
                       else                " = "
                     )
                    + pivot );
            }
        }
    }
}

Note that the conversion of Float or Double to Int must be explicit. (That hole in C++ always bugged me.) And I pushed the if-expression into the inside of the println call.
11:43:53 PM   comment/     



Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:39:24 .
Click here to send an email to the editor of this weblog.

May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun

Previous/Next