Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Java and Scala, a demonstration in code.
Author Message
wtd

Posted: 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.

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

Clayton

Posted: 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

Posted: 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

Posted: 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

Posted: 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

Posted: 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 19105 digits

 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

Posted: 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.

Scala (java) frontend and a haskell backend.. Its a prefect combo!
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 7 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: