I’m looking for the standard way to represent negative infinity in Lisp. Is there a symblic value which is recognised by Lisp’s arithmetic functions as less than all other numbers?
Specifically, I’m looking for an elegant way to write the following:
(defun largest (lst)
"Evaluates to the largest number in lst"
(if (null lst)
***negative-inifinity***
(max (car lst) (largest (cdr lst)))))
ANSI Common Lisp has
bignum, which can used to represent arbitrarily large numbers as long as you have enough space, but it doesn’t specify an “infinity” value. Some implementations may, but that’s not part of the standard.In your case, I think you’ve got to rethink your approach based on the purpose of your function: finding the largest number in a list. Trying to find the largest number in an empty list is invalid/nonsense, though, so you want to provide for that case. So you can define a precondition, and if it’s not met, return
nilor raise an error. Which in fact is what the built-in functionmaxdoes.EDIT: As pointed by Rainer Joswig, Common Lisp doesn’t allow arbitrarily long argument lists, thus it is best to use
reduceinstead ofapply.