Google Groups Home
Help | Sign in
pairp function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Francis Leboutte  
View profile
 More options Jun 26, 11:41 am
From: Francis Leboutte <f.lebou...@algo.be>
Date: Thu, 26 Jun 2008 17:41:11 +0200
Local: Thurs, Jun 26 2008 11:41 am
Subject: pairp function
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pete  
View profile
 More options Jul 8, 12:01 pm
From: Pete <peter.e.cl...@boeing.com>
Date: Tue, 8 Jul 2008 09:01:56 -0700 (PDT)
Local: Tues, Jul 8 2008 12:01 pm
Subject: Re: pairp function

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

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pete  
View profile
 More options Jul 8, 12:05 pm
From: Pete <peter.e.cl...@boeing.com>
Date: Tue, 8 Jul 2008 09:05:06 -0700 (PDT)
Local: Tues, Jul 8 2008 12:05 pm
Subject: Re: pairp function

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:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Leboutte  
View profile
 More options Jul 9, 2:46 am
From: Francis Leboutte <f.lebou...@algo.be>
Date: Wed, 09 Jul 2008 08:46:45 +0200
Local: Wed, Jul 9 2008 2:46 am
Subject: Re: pairp function
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 :


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pete  
View profile
 More options Jul 10, 12:56 pm
From: Pete <peter.e.cl...@boeing.com>
Date: Thu, 10 Jul 2008 09:56:39 -0700 (PDT)
Local: Thurs, Jul 10 2008 12:56 pm
Subject: Re: pairp function

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

On Jul 8, 11:46 pm, Francis Leboutte <f.lebou...@algo.be> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google