unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Chained Object Type Predicates
@ 2009-06-08 12:39 Nordlöw
  2009-06-08 12:58 ` Pascal J. Bourguignon
  2009-06-08 13:36 ` Lennart Borgman
  0 siblings, 2 replies; 3+ messages in thread
From: Nordlöw @ 2009-06-08 12:39 UTC (permalink / raw)
  To: help-gnu-emacs

How should i implement chained type predicate functions, for example

(defun list-of-numbers-p (object) (and (listp object) ...))

should return t if object is of type list (listp) and its elements in
turn all are of type string (stringp) otherwise nil.

Thanks in advance,
Per Nordlöw


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Chained Object Type Predicates
  2009-06-08 12:39 Chained Object Type Predicates Nordlöw
@ 2009-06-08 12:58 ` Pascal J. Bourguignon
  2009-06-08 13:36 ` Lennart Borgman
  1 sibling, 0 replies; 3+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-08 12:58 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> How should i implement chained type predicate functions, for example
>
> (defun list-of-numbers-p (object) (and (listp object) ...))
>
> should return t if object is of type list (listp) and its elements in
> turn all are of type string (stringp) otherwise nil.

Then why don't you call it list-of-strings-p ?


There are several ways to do so.

One way is to notice that a list is either nil (meaning the empty list)
or made of a first item followed by a rest sublist.
So you could write: 

(defun list-of-numbers-p (object)
    (or (null object)
        (and (consp object)
             (numberp (first object))
             (list-of-numbersp (rest object)))))

Another would be to use reduce:

(require 'cl)
(defun list-of-numbers-p (object)
  (and (listp object)
       (reduce (lambda (&optional (r rp) e)
                   (if rp (and r (numberp e)) t)) object)))



Finally, you may notice that in a list of numbers, every element is a number:

(require 'cl)
(defun list-of-numbers-p (object)
   (and (listp object) (every (function numberp) object)))




You may write a macro to build these functions automatically:

(defun proper-list-p (object)
  (labels ((step (slow fast)
             (cond ((eq slow fast)           nil) ; circular list
                    ((null (cdr fast))       t) 
                    ((atom (cdr fast))       nil) ; dotted list
                    ((null (cdr (cdr fast))) t) 
                    ((atom (cdr (cdr fast))) nil) ; dotted list 
                    (t (step (cdr slow) (cdr (cdr fast)))))))
   (cond            
      ((null object)       t)   ; empty list
      ((atom object)       nil) ; not a list
      ((null (cdr object)) t)   ; one-element list
      (t (step object (cdr object))))))

(defmacro proper-list-of (predicate)
   `(lambda (object) 
       (and (proper-list-p object)
            (every (function ,predicate) object))))


(funcall (proper-list-of integerp) '(1 2 3 4))             --> t
(funcall (proper-list-of integerp) '(1 2 "3" 4))           --> nil
(funcall (proper-list-of integerp) '(1 2 3 . 4))           --> nil
(funcall (proper-list-of integerp) '(1 2 #1=(3 4 . #1#)))  --> nil
         
-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Chained Object Type Predicates
  2009-06-08 12:39 Chained Object Type Predicates Nordlöw
  2009-06-08 12:58 ` Pascal J. Bourguignon
@ 2009-06-08 13:36 ` Lennart Borgman
  1 sibling, 0 replies; 3+ messages in thread
From: Lennart Borgman @ 2009-06-08 13:36 UTC (permalink / raw)
  To: Nordlöw; +Cc: help-gnu-emacs

On Mon, Jun 8, 2009 at 2:39 PM, Nordlöw<per.nordlow@gmail.com> wrote:
> How should i implement chained type predicate functions, for example
>
> (defun list-of-numbers-p (object) (and (listp object) ...))
>
> should return t if object is of type list (listp) and its elements in
> turn all are of type string (stringp) otherwise nil.

Maybe there is something special implemented for it, but otherwise
just loop over the list and jump out with catch/throw.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-06-08 13:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-08 12:39 Chained Object Type Predicates Nordlöw
2009-06-08 12:58 ` Pascal J. Bourguignon
2009-06-08 13:36 ` Lennart Borgman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).