all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Chained Object Type Predicates
Date: Mon, 08 Jun 2009 14:58:08 +0200	[thread overview]
Message-ID: <7cmy8inaqn.fsf@pbourguignon.anevia.com> (raw)
In-Reply-To: 80c94ce8-8e0b-4ebe-a575-f1f25a6fd734@n8g2000vbb.googlegroups.com

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__


  reply	other threads:[~2009-06-08 12:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-08 12:39 Chained Object Type Predicates Nordlöw
2009-06-08 12:58 ` Pascal J. Bourguignon [this message]
2009-06-08 13:36 ` Lennart Borgman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7cmy8inaqn.fsf@pbourguignon.anevia.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.