> 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 :
> >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:
> >> 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- Hide quoted text -
> - Show quoted text -