From: Michael O Shea
Subject: Position Predicate
Date: 
Message-ID: <885472740.2130837511@dejanews.com>
Hi,
I'm trying to use an implementation of Lisp which doesnt
have support for the Position Predicate (Gclisp).

Can someone give me the lisp code which would replicate
the position predicate, or point me somewhere that i would get it.

Thanks
Michael

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

From: Thomas A. Russ
Subject: Re: Position Predicate
Date: 
Message-ID: <ymien1z5dc4.fsf@sevak.isi.edu>
Michael O Shea <······@iol.ie> writes:
> Can someone give me the lisp code which would replicate
> the position predicate, or point me somewhere that i would get it.

I would think that writing one would be a good exercise in Lisp use.

Anyway, here is one version.  I don't offhand know if gclisp supports
the LABELS construct.  If not, you will have to introduce a separately
defined auxiliary function.

(defun position (object sequence)
  ;; Implementation of keywords left as an exercise -- I don't feel like
  ;;   doing THAT much work.
  (labels ((position-list (object sequence n)
	     ;; Recursively CDR down sequence, keeping count in N
             (cond ((null sequence) nil)
                   ((eq object (first sequence)) n)
                   (t (position-list object (rest sequence) (+ n 1))))))
    (cond ((null sequence) nil)
	  ((consp sequence) (position-list object sequence 0))
	  ((eq object (elt sequence 0)) 0)
	  (t (dotimes (i (- (length sequence) 1))
	       (when (eq object (elt sequence i))
		 (return-from position i)))
	      nil))))


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Barry Margolin
Subject: Re: Position Predicate
Date: 
Message-ID: <FE6y.68$Ps3.1481881@cam-news-reader1.bbnplanet.com>
In article <···············@sevak.isi.edu>,
Thomas A. Russ <···@sevak.isi.edu> wrote:
>    (cond ((null sequence) nil)
>	  ((consp sequence) (position-list object sequence 0))

If you change the type test to LISTP, you can combine these two (since
POSITION-LIST handles a null list).

>	  ((eq object (elt sequence 0)) 0)

This is wrong if the length is 0.  E.g. if SEQUENCE is a vector whose fill
pointer is 0, the 0th element should be ignored and NIL should be returned.
However, as I mention below, you don't need this clause.

>	  (t (dotimes (i (- (length sequence) 1))
>	       (when (eq object (elt sequence i))
>		 (return-from position i)))
>	      nil))))

Fencepost error alert (or maybe "former Fortran programmer alert")!  This
never checks the last element of the vector.  The last iteration of DOTIMES
will have I set to <count>-1, so you don't need to subtract 1 yourself.
(dotimes (i 0) ...) will execute zero times, so this will handle the
zero-length vector case, returning NIL.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.