[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functional languages ill-suited for large programs?

On 4. Nov 2003, at 18:03, Jan-Willem Maessen wrote:

> Pascal Costanza <costanza@web.de> responds:
>> On 1. Nov 2003, at 00:56, Jan-Willem Maessen wrote:
>>> peculier Car _ = Car
>>> peculier _ Truck = Truck
>>> peculier _ _      = error "Ack!"
>>> How would you write the above in CLOS?  How would you explain it?
>> What's wrong with:
>> (defmethod peculier ((car car) par2)
>>    (declare (ignore par2))
>>    car)
>> (defmethod peculier (par1 (truck truck))
>>    (declare (ignore par1))
>>    truck)
>> (defmethod peculier (par1 par2)
>>    (declare (ignore par1 par2))
>>    (error "Ack!"))
> What is (peculier Car Truck)?  In my code it's Car.  Can you explain
> in simple terms what I'd need to do to get the same answer in CLOS?
> Can you justify any redundancy you might introduce?

First, a minor correction: It seems to me that I need to quote the 
return values in the first two methods for peculier.  (Actually, I 
don't know exactly what you want from this function. What does 
"peculier" mean?)

Anyway, here is the complete program:

(defclass car () ())

(defclass truck () ())

(setf car (make-instance 'car))

(setf truck (make-instance 'truck))

(defmethod peculier ((car car) par2)
    (declare (ignore par2))

(defmethod peculier (par1 (truck truck))
    (declare (ignore par1))

(defmethod peculier (par1 par2)
    (declare (ignore par1 par2))
    (error "Ack!"))

? (peculier car truck)
? (peculier nil truck)
? (peculier nil nil)
 > Error: Ack!

This is because in Common Lisp, parameters for generic functions are 
examined from left to right. So I don't need to modify the code in any 
special way.