pairp function

19 views
Skip to first unread message

Francis Leboutte

unread,
Jun 26, 2008, 11:41:11 AM6/26/08
to km...@googlegroups.com
Hello,

Here is a version of the optimized pairp function that should work for any Lisp (I also get a bug with LispWorks 5.1):
- the declaration is now correct.
- the function works exactly like the original one.

;;; thing: should be anything but a dotted list
(defun pairp (thing)
(declare (optimize (speed 3) (safety 0)))
(and (listp thing) ; proper, dotted or circular list
thing
(let ((thing-cdr (cdr thing)))
(and thing-cdr
(null (cdr thing-cdr)))))) ; if thing is a dotted list: error


Francis

Pete

unread,
Jul 8, 2008, 12:01:56 PM7/8/08
to km-qa

Thanks, Francis, it will be in the next KM release. Best wishes! Pete

Pete

unread,
Jul 8, 2008, 12:05:06 PM7/8/08
to km-qa

Actually, Francis, the behavior isn't quite correct - unlike the
original, it (undesirably) generates an error (rather than NIL) when
the argument is a dotted pair:

CL-USER(4): (pairp '(a . b))
Error: Attempt to take the cdr of B which is not listp.
[condition type: TYPE-ERROR]

So I'll leave the original in place for now. Pete

On Jun 26, 8:41 am, Francis Leboutte <f.lebou...@algo.be> wrote:

Francis Leboutte

unread,
Jul 9, 2008, 2:46:45 AM7/9/08
to km...@googlegroups.com
No, the original function (which is also the one currently used in SBCL) generates an error:

KM 4 > (defun pairp (list)(and (listp list) (eq (length list) 2)))
pairp

KM 5 > (pairp '(1 . 2))

Error: In a call to length of (1 . 2), tail 2 is not a LIST.


This should be what you want:

;;; return T if thing is 2 elements proper list


(defun pairp (thing)
(declare (optimize (speed 3) (safety 0)))

(and (consp thing)
(let ((thing-cdr (cdr thing)))
(and (consp thing-cdr)
(null (cdr thing-cdr))))))

Francis


Le 8/07/2008 18:05, Pete écrivait :

Pete

unread,
Jul 10, 2008, 12:56:39 PM7/10/08
to km-qa

Oops you're right! My two versions have different functionality for
dotted pairs (should return NIL, not error). Thanks for the updated
version of your pairp, that works as intended now, it will be in the
next KM release in the next day or so.

Best wishes! Pete
> >> Francis- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages