[personal profile] flexibeast
Over the last week or so, i've finally taken the plunge and begun trying to actually write something in Common Lisp (CL), instead of just reading about it.

What i decided to program was something that i could write very quickly in Perl: a library of functions that perform interval arithmetic, a choice inspired by an article on the uses of interval arithmetic.

As the Wikipedia entry on interval arithmetic notes, multiplying two intervals requires finding the maximum of four numbers. "Okay," thought i, "i'm sure there's a CL function called MAXIMUM or something." Well, of course there is: it's actually called MAX. Except that it doesn't take a list or a vector or something of numbers as its argument; no, instead, it takes as arguments each number of the list whose maximum one wishes to find.

This means that you can't do this:

(max '(1 2 3 4 5))   (1)

Instead, if you want to pass MAX a list of arguments whose length will only be known at run-time, you have to do this:

(apply #'max '(1 2 3 4 5))   (2)

At least, that's the only way i can see of doing it. If that's true, it seems an oddly complicated way of performing what i would think is a common task: i've don't recall ever having written a program where i've had to find the maximum of a list of numbers whose length is known prior to run-time. And implementing a version of MAX that works in example (1) above is not particularly difficult:

(defun sensible-max (num-list)
  (let ((local-max 0))
    (dolist (item num-list local-max)
      (if (> item local-max)
        (setq local-max item)))))

i'm sure there are a number of implementation issues with this, particularly as num-list grows large; but i'm not sure there aren't implementation issues with the extant version of MAX - for example, what's the largest number of arguments that can be passed to a given function?

i can only assume that i'm naïvely missing something . . . .
 

Date: 2006-02-16 14:45 (UTC)
From: [identity profile] ubiquity.livejournal.com
I'm not so sure you're missing anything. One thing that CL is sorely lacking is a nice set of standard libraries. We implemented our own over the years, including, unsurprisingly, MAXIMUM. (:

Date: 2006-02-16 20:56 (UTC)
From: [identity profile] flexibeast.livejournal.com
:-)

So have you predominantly used CL in your work, or have you used other Lisp dialects as well? Or is the answer to that Top Secret? :-)

Date: 2006-02-16 21:07 (UTC)
From: [identity profile] ubiquity.livejournal.com
We use an in-house dialect of Lisp that's optimized for software engineering and translatability into C. For instance, it has private/protected/public implemented, and we got rid of some language constructs that were difficult to automatically translate into C.

Date: 2006-02-16 21:11 (UTC)
From: [identity profile] flexibeast.livejournal.com
Ooh, snazzy! :-)

That's a great pic of you in your icon, btw. :-)

Date: 2006-02-16 21:18 (UTC)
From: [identity profile] ubiquity.livejournal.com
Thank you! (:

Date: 2006-02-16 14:56 (UTC)
From: [identity profile] xach.livejournal.com
(defun list-max (list)
  (loop for i in list maximizing i))


Date: 2006-02-16 20:28 (UTC)
From: [identity profile] flexibeast.livejournal.com
Sorry, i don't understand what you're trying to say here?

Date: 2006-02-16 20:41 (UTC)
From: [identity profile] xach.livejournal.com
"Here is an alternative, concise function that finds the maximum numeric value in a list."
(deleted comment)

Date: 2006-02-16 20:45 (UTC)
From: [identity profile] xach.livejournal.com
See section 6.1 (http://www.lispworks.com/documentation/HyperSpec/Body/06_a.htm).

Date: 2006-02-16 20:48 (UTC)
From: [identity profile] flexibeast.livejournal.com
Ah, okay! i get it now - thanks. :-)

Date: 2006-02-16 20:45 (UTC)
From: [identity profile] flexibeast.livejournal.com
Oh, okay, thanks. But from what i can see, maximizing isn't listed in the Common Lisp HyperSpec? Is that function part of the ANSI standard?

Date: 2006-02-16 15:01 (UTC)
From: [identity profile] xach.livejournal.com
Also: (reduce #'max list)

There are a number of arithmetic operations that are defined only for arguments. There are also a number of standard ways to make those operations work on a list. It would be a little silly to have both + and list-+, max and list-max, * and list-*, etc.

Date: 2006-02-16 20:41 (UTC)
From: [identity profile] flexibeast.livejournal.com
Um, sorry, but i think you've missed the point i was trying to make. i don't know about you, but in my many years of coding, i have tended to need to use arithmetic operators on a set of arguments whose arity is known as i code; i have only rarely needed to, for example, sum an arbitrary number of arguments. However, whenever i've needed to find the maximum of a set of numbers, that set of numbers has rarely (if ever) been of a known arity whilst i was coding. So basically what i was saying was that i feel MAX should by default handle what for me is the common case - an arbitrarily-sized set of numbers. Particularly because it's actually a generalisation of the fixed-arguments case - having MAX accept an arbitrarily-sized list as an argument means that both the fixed-arity and unknown-arity cases can be accommodated without the programmer needing to do anything extra (such as using map or reduce) to get the job done.

Date: 2006-02-16 20:43 (UTC)
From: [identity profile] xach.livejournal.com
Welcome to Lisp! It's pretty common to use numeric functions like + on an arbitrary number of arguments, and most numeric functions work in the same way.

Date: 2006-02-16 20:53 (UTC)
From: [identity profile] flexibeast.livejournal.com
*nod* i suspected as much - that my common/rare cases are not correspondingly common/rare cases in the domains CL has been designed for. i imagine it might take a bit of time before i'm adjusted to the Lisp Way. :-) But so far, i'm enjoying the journey. :-)

Date: 2006-02-16 20:44 (UTC)
From: [identity profile] xach.livejournal.com
Also, if you have the arguments as variables, it's a bit wasteful to cons up a list to pass to MAX.

Date: 2006-02-16 20:54 (UTC)

Profile

flexibeast: Baphomet (Default)
flexibeast

Journal Tags

Style Credit

Powered by Dreamwidth Studios