GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Tuesday 4 May 2004
Chapter 3, Exercises 1-2, Thinking in Scala

I think I forgot to mention that I found THECLAPP's version of the Java/Lisp Exercises via this entry in Bill Clementson's excellent (mostly Lisp-related) weblog. Worth reading even if you're not that much of a Lisp fan.

And I didn't like putting the things off in separate files. So that will likely mean fewer blank lines and fewer comments within the code (because Radio thinks they're degenerate URLs), and the occasional < that I forget to escape. Perhaps later I'll go back and fix c2x7-9. And once I've collected a few more, I'll start putting up tarballs of the actual sources. Remember, I'm omitting the "package x;" statements that you might want to use in order that your source directory doesn't get cluttered with .class files.

This entry corresponds to 9920 in THECLAPP.

Chapter 3, Exercise 1

object c3x1 {
    def main ( args : Array[String] ) = {
        val x = 1;
        val y = 2;
        val z = 3;
        var a = x + y - 2/2 + z;
        Console.println( "a is " + a );
        a = x + (y - 2) / (2 + z);
        Console.println( "a is " + a );
    }
}

No comment.

Chapter 3, Exercise 2

object c3x2 {
    def ternary ( i: Int ) = {
        i * (if (i < 10) { 100 } else { 10 } )
    }
    def alternative ( i: Int ) = {
        if (i < 10) { i * 100 }
        else        { i * 10 }
    }
    def main ( args : Array[String] ) = {
        Console.println( ternary( 9 ) );
        Console.println( alternative( 11 ));
    }
}
No need to say "return" explicitly. Do note that I actually had to do something with the values returned from the ternary and alternative methods. The error message that I got when I did not do so was somewhat less than helpful: Exception in thread "main" java.lang.NoSuchMethodError: main

For what it's worth, I did try to define an actual "?" operator, first as a global operator?, then as a member of a new "xBoolean" class. And a new "xInt" class to go with it, and an auxiliary class to keep track of the values of the predicate and the then-part, and that auxiliary class was going to have a "|" operator (I figured that colon was too integrated into Scala syntax to be a good choice here) ... but I guess that'll have to wait until I understand Scala operator overloading a lot better.
11:34:59 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:38:56 .
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