Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java and Scala, a demonstration in code.
Index -> Programming, Java -> Java Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue Jan 09, 2007 12:52 am   Post subject: Java and Scala, a demonstration in code.

A little demonstration of Scala, and what it can do, with regards to recursion. Smile

Also a bit of comparison between Test.java and ScalaTest.scala.

code:
C:\Documents and Settings\Chris\My Documents\scala>type mymath\Factorial.scala
package mymath

import java.math.BigInteger
import BigInteger.ONE

object Factorial {
    def apply(n: Int): BigInteger = {
        def aux(n: BigInteger, acc: BigInteger): BigInteger = {
            val comparison = n compareTo ONE

            if (comparison == -1 || comparison == 0) acc
            else aux(n subtract ONE, acc multiply n)
        }

        aux(new BigInteger(n toString), ONE)
    }
}

C:\Documents and Settings\Chris\My Documents\scala>type Test.java
import mymath.Factorial;

public class Test {
    public static void main(String[] args) {
        System.out.println(Factorial.apply(400));
    }
}

C:\Documents and Settings\Chris\My Documents\scala>scalac -classpath . mymath\Factorial.scala

C:\Documents and Settings\Chris\My Documents\scala>javac -classpath . Test.java

C:\Documents and Settings\Chris\My Documents\scala>scala -classpath . Test
640345228466238952623479703195030058507025830260029594586844459428023971691868314362784786474632646762943505750358568108
482981628835174352289619886468029979373416541508381624264619423523070462443250151144486708906627739149181173319559964407
095496713452904770203224349112107975932807951015453726672516278778900093497637657103263503315339653498683868313393520243
737881577867915063118587026182701698197400629830253085912983461622723045583395207596115053022360868104332972551948526744
322324386699484224042325998055516106359423769613992319171340638589965379701478272066063202173794720103213566246138090779
423045973606995675958360961587151299138222865785795493616176544804532220078258184008484364155912294542753848035583745180
226759000613995601455952061272111929181050324910080000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000

C:\Documents and Settings\Chris\My Documents\scala>type ScalaTest.scala
import mymath.Factorial

object ScalaTest extends Application {
    Console println Factorial(400)
}

C:\Documents and Settings\Chris\My Documents\scala>scalac -classpath . ScalaTest.scala

C:\Documents and Settings\Chris\My Documents\scala>scala -classpath . ScalaTest
640345228466238952623479703195030058507025830260029594586844459428023971691868314362784786474632646762943505750358568108
482981628835174352289619886468029979373416541508381624264619423523070462443250151144486708906627739149181173319559964407
095496713452904770203224349112107975932807951015453726672516278778900093497637657103263503315339653498683868313393520243
737881577867915063118587026182701698197400629830253085912983461622723045583395207596115053022360868104332972551948526744
322324386699484224042325998055516106359423769613992319171340638589965379701478272066063202173794720103213566246138090779
423045973606995675958360961587151299138222865785795493616176544804532220078258184008484364155912294542753848035583745180
226759000613995601455952061272111929181050324910080000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000

C:\Documents and Settings\Chris\My Documents\scala>
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Tue Jan 09, 2007 1:01 am   Post subject: Re: Java and Scala, a demonstration in code.

is Scala an extension of sorts to Java? Either way, i fail to see what you are trying to show us, please enlighten me about what you are trying to show.
md




PostPosted: Tue Jan 09, 2007 1:12 am   Post subject: RE:Java and Scala, a demonstration in code.

Scala is another language that runs on the java virtual machine. It's considerably more powerful then Java.
wtd




PostPosted: Tue Jan 09, 2007 1:19 am   Post subject: RE:Java and Scala, a demonstration in code.

In Java, there is no tail-call optimization, so trying to find the factorial of large numbers would result in a stack overflow.

Scala can implement factorial recursively, and it has efficient runtime characteristics.
md




PostPosted: Tue Jan 09, 2007 1:21 am   Post subject: RE:Java and Scala, a demonstration in code.

tail-call optimizations are cool, very very nifty if you do any assembly level stuff as well
zylum




PostPosted: Tue Jan 09, 2007 2:45 am   Post subject: RE:Java and Scala, a demonstration in code.

my version of the recursive factorial function can calculate 5745! before a stack overflow occures.. I dont thing that is too bad Wink 19105 digits Very Happy

JAVA:
import java.math.*;

class Factorial
{
        public static void main (String [] args)
        {
        System.out.println(factorial(new BigInteger(args[0]), BigInteger.ONE).length());
        }
       
        static String factorial (BigInteger n, BigInteger ret)
        {
                if (n.compareTo(BigInteger.ZERO) == 0 || n.compareTo(BigInteger.ONE) == 0) return ret.toString();
                else return factorial(n.subtract(BigInteger.ONE), ret.multiply(n));
        }
 
}
rizzix




PostPosted: Thu Jan 11, 2007 7:27 pm   Post subject: RE:Java and Scala, a demonstration in code.

yea scala == fun.. i particularly like the pattern matching..

most of my newer projects are in scala. Smile

Scala (java) frontend and a haskell backend.. Its a prefect combo!
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: