944,157 Members | Top Members by Rank

Ad:
Nov 23rd, 2005
0

Lisp help - is this function right?

Expand Post »
Hello,

the listp function doesn't apply the full "list" rules.

Example

(cons 1 ( cons 2 3 ))

Isn't a list as the definition goes:

* either an empty list (nil)
* or a cons cell that has a list for a cdr

altough listp yelds true to that "list"

So I'm writing a function that will do the full thing

(defun isList (list)
   (if (null list)
        t
        (if (not (consp list ))
             nil
             (and t (isList (cdr list))))))

This is correct for the following examples

(cons 1 ( cons 2 ( cons 3 ( cons 4 5 ))))
returns nil
(cons 1 (cons 2 3))
returns nul
(cons 1 ( cons 2 ( cons 3 ( cons 4 nil ))))
returns t
(1 2)
returns t

It seams ok. Is it?

Thamls.
Similar Threads
Mr.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mr. is offline Offline
18 posts
since May 2005
Nov 23rd, 2005
3

Re: Lisp help - is this function right?

It looks like a fine implementation of the behavior of the Scheme function, LIST?. I don't know why you'd use (and t (isList (cdr list))) when that means the same thing as (isList (cdr list)).

However, Common Lisp (I'll assume that's the variant you're using, since you didn't specify) has a different meaning for listp.

Quote originally posted by Common Lisp the Language, 2nd Edition ...
listp object

listp is true if its argument is a cons or the empty list (), and otherwise is false. It does not check for whether the list is a ``true list'' (one terminated by nil) or a ``dotted list'' (one terminated by a non-null atom).
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: First time using awk, I have a couple questions
Next Thread in Legacy and Other Languages Forum Timeline: Ruby Language





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC