Difference between revisions of "Scheme for Java Programmer"

From CDOT Wiki
Jump to: navigation, search
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
{{Admon/obsolete}}
 +
 +
<h1>NexJ-Scheme combines the powers of Scheme and Java. You can get the best of both.</h1>
 +
 
<pre>
 
<pre>
 +
 
String
 
String
 
   
 
   
>>>> in Java
+
>>>>>>>> in Java
 
String st = new String("Seneca");
 
String st = new String("Seneca");
 
st = st.replace("e", "u");
 
st = st.replace("e", "u");
 
System.out.println(st);  // Sunuca
 
System.out.println(st);  // Sunuca
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(define st (java.lang.String'new "Seneca"))
 
(define st (java.lang.String'new "Seneca"))
 
(set! st (st'replace "e" "u" ))
 
(set! st (st'replace "e" "u" ))
Line 15: Line 20:
 
if
 
if
  
>>>> in Java
+
>>>>>>>> in Java
 
int x = 3, y = 5, large = 0;
 
int x = 3, y = 5, large = 0;
 
if (x > y)
 
if (x > y)
Line 24: Line 29:
 
System.out.print(large);  // 5
 
System.out.print(large);  // 5
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(define x 3)
 
(define x 3)
 
(define y 5)
 
(define y 5)
Line 38: Line 43:
 
for
 
for
  
>>>> in Java
+
>>>>>>>> in Java
 
for (int i = 0; i < 10; i++)
 
for (int i = 0; i < 10; i++)
 
     System.out.print(i);  // 0123456789
 
     System.out.print(i);  // 0123456789
  
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(for ((i 0)) (< i 10) (set! i (+ i 1))
 
(for ((i 0)) (< i 10) (set! i (+ i 1))
 
   (display i)  ; 0123456789
 
   (display i)  ; 0123456789
Line 50: Line 55:
 
While
 
While
  
>>>> in Java
+
>>>>>>>> in Java
 
int n = 0;
 
int n = 0;
 
while(n < 10){
 
while(n < 10){
Line 58: Line 63:
 
// 0123456789
 
// 0123456789
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(define n 0)
 
(define n 0)
  
Line 71: Line 76:
 
Switch Case
 
Switch Case
  
>>>> in Java
+
>>>>>>>> in Java
 
int x = 7;
 
int x = 7;
 
String st = "";
 
String st = "";
Line 82: Line 87:
 
System.out.println(st);
 
System.out.println(st);
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(define x 7)
 
(define x 7)
 
(define st "")
 
(define st "")
Line 99: Line 104:
 
Iterator
 
Iterator
  
>>>> in Java
+
>>>>>>>> in Java
 
ArrayList numbers = new ArrayList();
 
ArrayList numbers = new ArrayList();
  
Line 109: Line 114:
  
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(import 'java.util.ArrayList)
 
(import 'java.util.ArrayList)
 
(define numbers (java.util.ArrayList'new))
 
(define numbers (java.util.ArrayList'new))
Line 124: Line 129:
 
Try and Catch
 
Try and Catch
  
>>>> in Java
+
>>>>>>>> in Java
  
 
try {
 
try {
Line 136: Line 141:
 
//  finally
 
//  finally
  
>>>> in Scheme
+
>>>>>>>> in Scheme
 
(try
 
(try
 
   (define x (/ 1 0))
 
   (define x (/ 1 0))
Line 142: Line 147:
 
   (display "finally")
 
   (display "finally")
 
)
 
)
; final
+
; finally
 
; #<java.lang.ArithmeticException: / by zero>
 
; #<java.lang.ArithmeticException: / by zero>
 
 
 
  
  
 
</pre>
 
</pre>

Latest revision as of 21:45, 26 January 2014

Important.png
This page may be obsolete.
It contains historical information.

NexJ-Scheme combines the powers of Scheme and Java. You can get the best of both.


String
 
>>>>>>>> in Java
String st = new String("Seneca");
st = st.replace("e", "u");
System.out.println(st);  // Sunuca

>>>>>>>> in Scheme
(define st (java.lang.String'new "Seneca"))
(set! st (st'replace "e" "u" ))
(display st)  ; Sunuca

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
if

>>>>>>>> in Java
int x = 3, y = 5, large = 0;
if (x > y)
    large = x;
else
    large = y;

System.out.print(large);  // 5

>>>>>>>> in Scheme
(define x 3)
(define y 5)
(define large 0)

(if (> x y)
   (set! large x)
   (set! large y)
)
(display large) ; 5

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
for

>>>>>>>> in Java
for (int i = 0; i < 10; i++)
    System.out.print(i);  // 0123456789


>>>>>>>> in Scheme
(for ((i 0)) (< i 10) (set! i (+ i 1))
   (display i)  ; 0123456789
)
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
While

>>>>>>>> in Java
int n = 0;
while(n < 10){
    System.out.print(n);
    n++;
}
// 0123456789

>>>>>>>> in Scheme
(define n 0)

(while (< n 10)
   (display n)
   (set! n (+ n 1))
)
; 0123456789


- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Switch Case

>>>>>>>> in Java
int x = 7;
String st = "";
switch (x){
    case 1: st = "one";    break;
    case 3: st = "three";  break;
    case 7: st = "seven";  break;
    default: st = "else";
}
System.out.println(st);

>>>>>>>> in Scheme
(define x 7)
(define st "")

(case x
   ((1) (set! st 'one))
   ((3) (set! st 'three))
   ((7) (set! st 'seven))
   (else (set! st 'else))
)

(display st) ; seven


- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Iterator

>>>>>>>> in Java
ArrayList numbers = new ArrayList();

for (int i = 0; i < 10; i++)
    numbers.add(i);

for (Iterator iterator = numbers.iterator(); iterator.hasNext();)
    System.out.print(iterator.next());    //  0123456789


>>>>>>>> in Scheme
(import 'java.util.ArrayList)
(define numbers (java.util.ArrayList'new))

(for ((i 0)) (< i 10) (set! i (+ i 1))
   (numbers'add i)
)

(for ((it (numbers'iterator))) (it'hasNext) ()
   (write (it'next)) ; 0123456789
)

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Try and Catch

>>>>>>>> in Java

try {
    int x = 1 /0;
} catch (ArithmeticException e) {
    e.printStackTrace();
} finally{
    System.out.println("finally");
}
// java.lang.ArithmeticException: / by zero
//  finally

>>>>>>>> in Scheme
(try
   (define x (/ 1 0))
   (lambda (e) e)
   (display "finally")
)
; finally
; #<java.lang.ArithmeticException: / by zero>