---
// DemonstratePrecedence.java
// chapter 3, exercise 1class DemonstratePrecedence {
public static void main(String[] args) {
int a, x, y, z;
x = 1;
y = 2;
z = 3;a = x + y - 2/2 + z;
System.out.println( "a is " + a );
a = x + (y - 2) / (2 + z);
System.out.println( "a is " + a );
}
}
---
Chapter 3, ex 1, Lisp version
---
;;;; chapter 3, ex 1
(defun demonstrate-precedence ()
"Not really much point to this one, since Lisp doesn't actually HAVE precedence,
but I'll do it anyway."
(let (a
(x 1)
(y 2)
(z 3))
(setf a (+ x y z (- (/ 2 2))))
(format t "a is ~a~%" a)
(setf a (+ x (/ (- y 2) (+ 2 z))))
(format t "a is ~a~%" a)))
---
09:07 -- starting 3.2, Java version
09:11 -- done
---
// ch3_ex2.java
// chapter 3, exercise 2class ch3_ex2 {
static int ternary( int i ) {
return i < 10 ? i * 100 : i * 10;
}static int alternative( int i ) {
if (i < 10)
return i * 100;
else
return i * 10;
}public static void main( String[] args ) {
ternary( 9 );// this one goes to 11
alternative( 11 );
}
}
---
09:12 -- starting 3.2, Lisp version
09:17 -- done
---
;;;; chapter 2, ex 2
(defun ternary (i)
;;; could use flet for ternary and alternative, actually
(if (< i 10) (* i 100) (* i 10)))
(defun alternative (i)
(cond ((< i 10) (* i 100))
(t (* i 10))))
(ternary 9)
(alternative 11)
---
At first I did ternary and alternative exactly the same, but did the "if" on one line in ternary and on three in alternative, just to be a smartass. ;) But I changed my mind and modified it a bit.
As with many of these exercises so far, the "do this just to get a feeling for the language" spirit of the exercise doesn't really translate well from Java to Lisp, at least to me.