From: Robert Maas, see http://tinyurl.com/uh3t
Subject: Unexpected result from MEMBER with :KEY parameter, bug or misunderstandig?
Date:
Message-ID: <rem-2008mar25-001@yahoo.com>
My program went into break loop, because it tried to do arithmetic
on NIL, because at a higher level it made a wrong decision and
executed a function (to test triangles formed by the new link not
yet in the graph and adjacent links already in
the graph, i.e. new link A-B and adjacent link
B-C hence third side of triangle A-C)
that shouldn't have been executed at that time (because the link
was *already* in the graph, so it was adjacent to itself).
(Aside for anyone *really* curious how NIL was generated:
If the new edge A-B and the adjacent edge B-C are regarded as sets,
then the vertices of the third edge may be computed via SET-DIFFERENCE,
but if the adjacent edge is the same, then SET-DIFFERENCE returns NIL.)
It made this wrong decision because MEMBER returned NIL (link not
yet in graph) when AFAIK it should have returned non-NIL result
(link already in graph).
From the break I discovered:
6] thislink
(0.006337395 #:AC #:AD)
;(CartesianDistance Vertex1 Vertex2)
6] g*links-in-graph
((0.044232372 #:AD #:AK) (0.030931061 #:AF #:AJ) (0.019626398 #:AG #:AJ)
(0.01561546 #:AB #:AI) (0.03120384 #:AB #:AG) (0.040734217 #:AA #:AF)
(0.009166301 #:AC #:AF) (0.032245275 #:AD #:AE) (0.006337395 #:AC #:AD)
(0.041924037 #:AA #:AB))
6] (nth 8 g*links-in-graph)
(0.006337395 #:AC #:AD)
6] (mapcar #'eq thislink (nth 8 g*links-in-graph))
(NIL T T)
6] (mapcar #'eql thislink (nth 8 g*links-in-graph))
(T T T)
6] (equal (cdr thislink) (cdr (nth 8 g*links-in-graph)))
T
6] (member thislink g*links-in-graph :test #'equal :key #'cdr)
NIL
;I was expecting this result to be non-NIL i.e. true, specifically:
; ((0.006337395 #:AC #:AD) (0.041924037 #:AA #:AB))
;I'm using the :KEY parameter in case the floating point values are
; slightly different due to different Cartesian-distance calculation
; depending on which of the two vertices is first.
6] (member thislink g*links-in-graph :test #'equal)
((0.006337395 #:AC #:AD) (0.041924037 #:AA #:AB))
;But that works!! Why the difference? Bug in CMU Common Lisp 18b,
; or I misunderstand how the :KEY parameter is supposed to work with MEMBER?
In article <·················@yahoo.com>,
·······@yahoo.com (Robert Maas, see http://tinyurl.com/uh3t) wrote:
> 6] (equal (cdr thislink) (cdr (nth 8 g*links-in-graph)))
>
> T
>
> 6] (member thislink g*links-in-graph :test #'equal :key #'cdr)
>
> NIL
> ;I was expecting this result to be non-NIL i.e. true, specifically:
> ; ((0.006337395 #:AC #:AD) (0.041924037 #:AA #:AB))
> ;I'm using the :KEY parameter in case the floating point values are
> ; slightly different due to different Cartesian-distance calculation
> ; depending on which of the two vertices is first.
The :KEY function is only applied to the elements of the list, NOT to
the item you're searching for. So you need to use:
(member (cdr thislink) g*links-in-graph :key #'cdr)
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***