commit 95531fb330388effe89f9bfff315d59e60b59a2f
parent 2004f07abad444dbed83584edd10d32e4d3a227b
Author: tomas <tomas@logand.com>
Date: Fri, 23 Oct 2009 20:38:17 +0200
comparison functions implemented
Diffstat:
M | java.wl | | | 19 | +++++++++++++++++++ |
M | wl.java | | | 47 | ++++++++++++++++++++++++++++++++++++++--------- |
2 files changed, 57 insertions(+), 9 deletions(-)
diff --git a/java.wl b/java.wl
@@ -439,3 +439,22 @@
(de even (N) (= (% N 2) 0))
(def 'odd (oq not even))
+
+(de lt0 (X) (and (num? X) (< X 0)))
+(de gt0 (X) (and (num? X) (< 0 X)))
+(de le0 (X) (and (num? X) (or (< X 0) (= 0 X))))
+(de ge0 (X) (and (num? X) (or (< 0 X) (= 0 X))))
+
+(de foldln (F A L)
+ (let Z T
+ (loop
+ (NIL (and (pair L) (setq Z (F A (car L)))) Z)
+ (setq A (pop 'L)) ) ) )
+(de foldln1 (F L) (foldln F (pop 'L) L))
+
+(de > @ (foldln1 '((X Y) (< Y X)) (rest)))
+(de <= @ (foldln1 '((X Y) (or (< X Y) (= X Y))) (rest)))
+(de >= @ (foldln1 '((X Y) (or (< Y X) (= X Y))) (rest)))
+(de <> @ (not (pass =)))
+
+(de abs (N) (if (lt0 N) (- N) N))
diff --git a/wl.java b/wl.java
@@ -586,12 +586,29 @@ class wl implements Runnable {
}
Env.val(X);
}
- boolean equal(Any X, Any Y) {
+ boolean eq(Any X, Any Y) {
boolean z = true;
- if(X.isCons()) z = equal(X.car(), Y.car()) && equal(X.cdr(), Y.cdr());
+ if(X.isCons()) z = Y.isCons() && eq(X.car(), Y.car()) && eq(X.cdr(), Y.cdr());
else if(X.isSym()) z = Y.isSym() && X == Y;
else if(X.isObj()) z = Y.isObj() && X.obj().equals(Y.obj());
- else err("Don't know how to equal");
+ else err("Don't know how to eq");
+ return z;
+ }
+ boolean lt(Any X, Any Y) { // (NIL 123 DEF "abc" (d e f) T)
+ boolean z = true;
+ if(NIL == Y || T == X) z = false;
+ else if(NIL == X || T == Y) z = true;
+ else if(X.isOnum())
+ z = !Y.isOnum() ||
+ ((BigInteger) X.obj()).compareTo((BigInteger) Y.obj()) < 0;
+ else if(X.isSym()) z = Y.isSym() ? X.nm().compareTo(Y.nm()) < 0 : !Y.isOnum();
+ else if(X.isOstr())
+ z = Y.isOstr() ? ((String) X.obj()).compareTo((String) Y.obj()) < 0
+ : Y.isCons();
+ else if(X.isCons())
+ z = Y.isCons() &&
+ (lt(X.car(), Y.car()) || eq(X.car(), Y.car()) && lt(X.cdr(), Y.cdr()));
+ else err("Don't know how to lt");
return z;
}
@@ -689,7 +706,19 @@ class wl implements Runnable {
if(I.isCons()) {
Any X = eval(I.car());
for(I = I.cdr(); I.isCons(); I = I.cdr())
- if(!equal(X, eval(I.car()))) return NIL;
+ if(!eq(X, eval(I.car()))) return NIL;
+ }
+ return T;
+ }});
+ fn("<", new Fn() {public Any fn(Any E) {
+ Any I = E.cdr();
+ if(I.isCons()) {
+ Any X = eval(I.car());
+ for(I = I.cdr(); I.isCons(); I = I.cdr()) {
+ Any Y = eval(I.car());
+ if(!lt(X, Y)) return NIL;
+ X = Y;
+ }
}
return T;
}});
@@ -837,6 +866,9 @@ class wl implements Runnable {
fn("num?", new Fn() {public Any fn(Any E) {
return eval(E.cdr().car()).isOnum() ? T : NIL;
}});
+ fn("obj?", new Fn() {public Any fn(Any E) {
+ return eval(E.cdr().car()).isObj() ? T : NIL;
+ }});
fn("let", new Fn() {public Any fn(Any E) {
Any Z = NIL;
Any I = E.cdr();
@@ -1065,11 +1097,8 @@ class wl implements Runnable {
Any F = I.car();
Any P = I.cdr();
Any Z = NIL;
- try {
- Z = xrun(P);
- } finally {
- eval(F);
- }
+ try {Z = xrun(P);}
+ finally {eval(F);}
return Z;
}});
fn("read", new Fn() {public Any fn(Any E) { // TODO