Hi Tomas,

> As the list being sorted is finite, it must be possible to come up
> with a unary weighting function.  The question is whether the unary
> function is always "better" then the binary predicate?

I do believe so. The unary function is simpler (as you see from the
previous examples), and faster (because the "retrieval" code, which
usually takes more time than the actual comparison, is called O(N)
times, as opposed to the binary predicate which is called more often,
and on top of that calls the "retrieval" code twice on each invocation).


> I updated http://logand.com/picoWiki/multimethods which I didn't
> manage to do last time as the internet in the internet cafe went
> down:-(

;-)


> (de subclass? (X Y)
>    (if (pair Y)
>       (loop
>          (NIL Y T)
>          (NIL (subclass? X (pop 'Y))) )
>       (let Q (if (pair X) X (val X)) # dfs
>          (use H
>             (loop
>                (NIL Q)
>                (setq H (pop 'Q))
>                (T (= H Y) T)
>                (for HH (val H)
>                   (push 'Q HH) ) ) ) ) ) )

BTW, this is almost the same as what 'isa' does. So it looks like that
instead of

   (subclass? LH RH)

you could simply write

   (isa RH LH)


> (de mmLt (L R)
>    (use (LH RH)
>       (loop
>          (NIL (or L R))
>          (setq LH (pop 'L) RH (pop 'R))
>          (T (and (not LH) RH) NIL)
>          (T
>           (when LH
>              (or (not RH) (and (<> LH RH) (subclass? LH RH))) )
>           T ) ) ) )
> ...
> I admit I cannot see a way of defining a unary weighting function
> easily.  Maybe this is the challenge, "switching" from binary to unary
> function, in this case?

I don't completely understand the exact ordering requirements, but I
feel that something like the following should work:

If you have a function 'rankClass' which returns the depth where a class
in the hierarchy is located

   (de rankClass (Cls)
      (cond
         ((not Cls) 0)
         ((atom Cls) (rankClass (type Cls)))
         (T
            (dec
               (min
                  (rankClass (car Cls))
                  (rankClass (cdr Cls)) ) ) ) ) )

then you could sort a list of the form

   (setq Table
      (quote
         (((+Cls1 +Cls2) (+Cls3 +Cls4)) ..)
         (((+Cls5 +Cls6) (+Cls7 +Cls8)) ..)
         ... ) )

with a function returning a list of lists of weights

   (by
      '((Lst)
         (mapcar '((L) (mapcar rankClass L)) (car Lst)) )
      sort
      Table )


> I would be curious to compare the running times for the multimethod
> example, if there is a reasonably "simple" unary function, it should
> be significantly faster provided there are many multimethods;-) (Even
> though the speed does not matter in this case, as sorting takes place
> during load time/method definition, not application.)

That's right, but interesting anyway ;-)

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe

Reply via email to