On Thu, 24 Apr 2008, Volkan YAZICI <[EMAIL PROTECTED]> writes:
> Ağız tadınıza uygun olarak KEY ve TEST seçeneklerini de -- POSITION
> işlevinde olduğu gibi -- POSITIONS'a da ekleyebilirsiniz.
(defun positions (item sequence &key (start 0) end (key #'identity) (test
#'eql))
"Return list of positions of ITEM occuring in SEQUENCE."
(check-type sequence (or vector list))
(let ((get-next-item
(cond ((vectorp sequence)
(let ((index 0)
(size (length sequence)))
(lambda ()
(when (< index size)
(prog1 (aref sequence index)
(incf index))))))
((listp sequence)
(lambda () (pop sequence))))))
(labels ((collect (position accum)
(let ((candidate (funcall get-next-item)))
(cond ((or (null candidate)
(and end (<= end position)))
accum)
((and (<= start position)
(funcall test item (funcall key candidate)))
(collect (1+ position) (cons position accum)))
(t
(collect (1+ position) accum))))))
(nreverse (collect 0 nil)))))
(positions 1 '(2 1 3 4 5 11 6 7 1 9))
(positions 1 #(2 1 3 4 5 11 6 7 1 9))
(positions #\a "Yok dahA neler Artık!" :test #'char-equal :key #'char-downcase)
Tabii ki kullanılmayan START, END, KEY ve TEST değişkenlerinin her
adımda bir yavaşlığa neden olacağı aşikardır. Fakat çoğu CL
implementasyonu (örneğin SBCL) bu tür ölü kodları optimize edebilmekte.
İyi çalışmalar.
_______________________________________________
cs-lisp mailing list
[email protected]
http://church.cs.bilgi.edu.tr/lcg
http://cs.bilgi.edu.tr/mailman/listinfo/cs-lisp