Created
May 13, 2012 09:35
-
-
Save komamitsu/2687186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hoge { | |
def fib(n: Int, i: Int, a: Int, b: Int) : Int = { | |
if (i >= n) b | |
else fib(n, i + 1, b, a + b) | |
} | |
def simple_tailrec(n: Int) : Int = { | |
if (n < 0) n else simple_tailrec(n - 1) | |
} | |
} | |
object Hoge { | |
def fib(n: Int, i: Int, a: Int, b: Int) : Int = { | |
if (i >= n) b | |
else fib(n, i + 1, b, a + b) | |
} | |
def simple_tailrec(n: Int) : Int = { | |
if (n < 0) n else simple_tailrec(n - 1) | |
} | |
def main(args: Array[String]) = { | |
val loop_count = 10000 | |
println(fib(loop_count, 1, 0, 1)) | |
println(simple_tailrec(loop_count)) | |
val hoge = new Hoge | |
println(hoge.fib(loop_count, 1, 0, 1)) | |
println(hoge.simple_tailrec(loop_count)) | |
} | |
} | |
$ komamitsu@carrot ~/lab/scala/tailrec $ scalac Hoge.scala | |
komamitsu@carrot ~/lab/scala/tailrec $ ls -ltra | |
total 20 | |
drwxr-xr-x 16 komamitsu komamitsu 4096 2012-05-13 18:25 .. | |
-rw-rw-r-- 1 komamitsu komamitsu 682 2012-05-13 18:28 Hoge.scala | |
-rw-rw-r-- 1 komamitsu komamitsu 1107 2012-05-13 18:29 Hoge$.class | |
drwxrwxr-x 2 komamitsu komamitsu 4096 2012-05-13 18:29 . | |
-rw-rw-r-- 1 komamitsu komamitsu 1257 2012-05-13 18:29 Hoge.class | |
komamitsu@carrot ~/lab/scala/tailrec $ scala Hoge | |
1242044891 | |
-1 | |
java.lang.StackOverflowError | |
at Hoge.fib(Hoge.scala:3) | |
at Hoge.fib(Hoge.scala:4) | |
at Hoge.fib(Hoge.scala:4) | |
at Hoge.fib(Hoge.scala:4) | |
: | |
snip | |
: | |
komamitsu@carrot ~/lab/scala/tailrec $ javap -c Hoge | |
Compiled from "Hoge.scala" | |
public class Hoge extends java.lang.Object implements scala.ScalaObject{ | |
public static final void main(java.lang.String[]); | |
Code: | |
0: getstatic #11; //Field Hoge$.MODULE$:LHoge$; | |
3: aload_0 | |
4: invokevirtual #13; //Method Hoge$.main:([Ljava/lang/String;)V | |
7: return | |
public int fib(int, int, int, int); | |
Code: | |
0: iload_2 | |
1: iload_1 | |
2: if_icmplt 10 | |
5: iload 4 | |
7: goto 24 | |
10: aload_0 | |
11: iload_1 | |
12: iload_2 | |
13: iconst_1 | |
14: iadd | |
15: iload 4 | |
17: iload_3 | |
18: iload 4 | |
20: iadd | |
21: invokevirtual #20; //Method fib:(IIII)I | |
24: ireturn | |
public int simple_tailrec(int); | |
Code: | |
0: iload_1 | |
1: iconst_0 | |
2: if_icmpge 9 | |
5: iload_1 | |
6: goto 16 | |
9: aload_0 | |
10: iload_1 | |
11: iconst_1 | |
12: isub | |
13: invokevirtual #32; //Method simple_tailrec:(I)I | |
16: ireturn | |
public Hoge(); | |
Code: | |
0: aload_0 | |
1: invokespecial #38; //Method java/lang/Object."<init>":()V | |
4: return | |
} | |
$ komamitsu@carrot ~/lab/scala/tailrec $ javap -c Hoge$ | |
Compiled from "Hoge.scala" | |
public final class Hoge$ extends java.lang.Object implements scala.ScalaObject{ | |
public static final Hoge$ MODULE$; | |
public static {}; | |
Code: | |
0: new #9; //class Hoge$ | |
3: invokespecial #12; //Method "<init>":()V | |
6: return | |
public int fib(int, int, int, int); | |
Code: | |
0: iload_2 | |
1: iload_1 | |
2: if_icmplt 8 | |
5: iload 4 | |
7: ireturn | |
8: iload_2 | |
9: iconst_1 | |
10: iadd | |
11: iload 4 | |
13: iload_3 | |
14: iload 4 | |
16: iadd | |
17: istore 4 | |
19: istore_3 | |
20: istore_2 | |
21: goto 0 | |
public int simple_tailrec(int); | |
Code: | |
0: iload_1 | |
1: iconst_0 | |
2: if_icmpge 7 | |
5: iload_1 | |
6: ireturn | |
7: iload_1 | |
8: iconst_1 | |
9: isub | |
10: istore_1 | |
11: goto 0 | |
public void main(java.lang.String[]); | |
Code: | |
0: sipush 10000 | |
3: istore_2 | |
4: getstatic #31; //Field scala/Predef$.MODULE$:Lscala/Predef$; | |
7: aload_0 | |
8: iload_2 | |
9: iconst_1 | |
10: iconst_0 | |
11: iconst_1 | |
12: invokevirtual #33; //Method fib:(IIII)I | |
15: invokestatic #39; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; | |
18: invokevirtual #43; //Method scala/Predef$.println:(Ljava/lang/Object;)V | |
21: getstatic #31; //Field scala/Predef$.MODULE$:Lscala/Predef$; | |
24: aload_0 | |
25: iload_2 | |
26: invokevirtual #45; //Method simple_tailrec:(I)I | |
29: invokestatic #39; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; | |
32: invokevirtual #43; //Method scala/Predef$.println:(Ljava/lang/Object;)V | |
35: new #47; //class Hoge | |
38: dup | |
39: invokespecial #48; //Method Hoge."<init>":()V | |
42: astore_3 | |
43: getstatic #31; //Field scala/Predef$.MODULE$:Lscala/Predef$; | |
46: aload_3 | |
47: iload_2 | |
48: iconst_1 | |
49: iconst_0 | |
50: iconst_1 | |
51: invokevirtual #49; //Method Hoge.fib:(IIII)I | |
54: invokestatic #39; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; | |
57: invokevirtual #43; //Method scala/Predef$.println:(Ljava/lang/Object;)V | |
60: getstatic #31; //Field scala/Predef$.MODULE$:Lscala/Predef$; | |
63: aload_3 | |
64: iload_2 | |
65: invokevirtual #50; //Method Hoge.simple_tailrec:(I)I | |
68: invokestatic #39; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; | |
71: invokevirtual #43; //Method scala/Predef$.println:(Ljava/lang/Object;)V | |
74: return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment