LISP apply question

From: jennyjenny (jennyjennydz_at_yahoo.com)
Date: 01/19/04


Date: 18 Jan 2004 15:19:12 -0800

Hello, complete LISP newbie here.

***Background - skip if you want
I am working on several high level math and statistics classes for
PHP. Right now I am working on a binomial linear regression
implementation. As background, I am studying implementations in other
languages. One which I found source code for the algorithm is in LISP.

In order to understand the algorithm I need to decipher certain
elements of the code. So, I have spent the last two days reading code
samples and an assortment of online resources. Online I have read
large parts (of the applicable sections) of Successful LISP, the
Common LISP HyperSpec, and several other sites as well.

I program fluently in PHP and Java for the web and C/C++ offline. I
have no LISP experience prior to 3 days ago.
***End of background stuff

Right now I have basically one question, well to be honest not knowing
LISP very well it may be multiple questions.

Here is the line of code I'm trying to translate into unLISPed
english.

**start of code chunk
(defun binomialreg-model (x y n &rest args)
"Args: (x y n &rest args)
Returns a binomial regression model. Accepts :LINK, :OFFSET and
:VERBOSE
keywords in addition to the keywords accepted by regression-model."
  (apply #'send binomialreg-proto :new :x x :y y :trials n args))
**end of code chunk

Here's what I know:
This is a Constructor function named binomialreg-model that accepts as
arguements 3 required parameters, x, y, and n and that potentially
accepts an unspecified number of additonal paraments, listed as args.
The function will return the object so created.

This is a object is an instance of a prototype called
binomialreg-proto.

Here's what I think:
To constuct the object, the function uses 1 line - the (apply .....)
line.
That line references a function send. That function somehow is applied
to the trailing args.
The reference binomialreg-proto refers to the prototype for the
binomialreg-model model.

Here's where I'm lost after reading the apply examples I've found:
Basically, I'm not sure what its doing.

Is it doing (send binomialreg-proto :new <args>) with all the things
after :new as <args>?
Is it doing some sort of sequence like:
 (send binomialreg-proto :new)
 (send binomialreg-proto :x)
 (send binomialreg-proto x)
 and so on...
Is it doing the above series all at once?
Is it doing something completely different?

This whole apply thing is thoroughly confusing me and unfortunately
for me the online examples make little sense. I mean really, the
examples start with
(setq f '+) => +
(apply f '(1 2)) => 3
which seems unusually silly to me. Why not just (+ 1 2)?

 Then, it dives all the way to the deep end with
(defun foo (size &rest keys &key double &allow-other-keys)
   (let ((v (apply #'make-array size :allow-other-keys t keys)))
     (if double (concatenate (type-of v) v v) v)))
 (foo 4 :initial-contents '(a b c d) :double t)
    => #(A B C D A B C D)
which has 50 things going on at once and no clear point as to what is
the apply doing.

Any help on explaining this in nonLISP terms would be greatly
appreciated.

Jenny