#LISP ???
10 messages in this thread
I have a list of reals assigned to s1. How can I find the minimum value in
that list s1 ? Example:
s1 = (1.2 3.4 5 6.7) Find the Minimum = 1.2 The MIN function
will not work on a list – am I missing a clever programming technique to
accomplish this ? If I am not missing a technique – what a "neat" feature
this would be in future LISP enhancements !
Dave Maki
David —
Try this:
..
(setq lst_of_reals (list 1.0 2.0 3.0 4.0 0.5))
..
(setq min_of_lst_of_reals (eval (cons 'min lst)))
..
As you can see, we use CONS to place the atom MIN at the front of
the list of reals, and then use EVAL to force an explicit evaluation of
the new list. This allows the MIN function to work as documented, while
allowing us the option of applying MIN to a separate list of reals.
— Brad
..
I like that – THANX ! Dave Maki
David – Here's another method involving a recusrive "mymin" function:
(defun mymin (a)
(cond
((> (length a) 2) (mymin (cons (min (car a) (cadr a)) (cddr a))))
((= (length a) 2) (min (car a) (cadr a)))
(T "Argument must be list of 2 or more numbers")
)
)
Assuming you've done something like (setq s1 (list 1.2 3.4 5 6.7)), you'd use
this thing by entering (mymin s1). "mymin" compares the first two numbers in
the list, and calls itself again with the smaller of these at the head of the
remaining list. This continues until there are only two items left in the
list.
Brad's method is simpler, but I didn't see his until I was about to send
mine. Oh well, it was a nice exercise.
.
THANX –
You are right – Brad's is easier – but I like to see how two
different people handle the same problem.
Dave Maki
David,
The function you are looking for is as follows:
(defun minlist (alist)
apply 'min alist)
)
Phil Kreiker, Looking Glass Microproducts
Phil,
Thanx ! I hyave received three different solutions to the same
problem – talk about response !
Dave Maki
David,
The function you are looking for is
(defun minlist (alist)
(apply 'min alist)
)
My last message omitted a (. Phil
Give Phil the prize for the best (and most efficient) answer! Phil, are you
gonna be at ACAD Expo??? Rusty
Rusty,
Yes, I'll be arriving in Chicago @5AM Monday, and attending virtually
all sessions. Departing Chicago early Friday AM. Staying at Hilton.
— Phil Kreiker, Looking Glass Microproducts