Another handy dandy conditional

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

Another handy dandy conditional

Post by Jeremy Dunn »

Here is a conditional form that I find very useful

Code: Select all

(define (<=> x y body1 body2 body3)
  (if (list? x)(setq x (length x)))
  (if (list? y)(setq y (length y)))
  (eval (if (< x y) body1
            (= x y) body2
            body3
        )))
This is a conditional with three bodies. X and Y can either be a list or a number. If X or Y is a list then the length of the list is used for comparison. The conditional looks at the two numbers X and Y. If X<Y body1 is executed elseif X=Y body2 is executed else body3 is executed. This happens a lot and I wish I would have thought of this a long time ago.

Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Re: Another handy dandy conditional

Post by Cyril »

Jeremy Dunn wrote:This is a conditional with three bodies.
It is only me who remembers FORTRAN 66 and arithmetic IF? ;-)
With newLISP you can grow your lists from the right side!

Locked