Sujet : Re: separating words
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 07. Sep 2024, 07:25:48
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vbgo4o$17c61$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Edward Fagan wrote:
Something like this function will tell you the position of
the last comma in a string.
(defun find-last-comma (s)
(loop for n from 0 below (length s)
if (char= (aref s n) #\,)
do
(if (and (loop for n downfrom (1- (length s)) to 0
if (char= (aref n s) #\,)
return n
finally (return nil))
(= n (loop for n downfrom (1- (length s)) to 0
if (char= (aref s n) #\,)
return n
finally (return nil))))
(return-from find-last-coma n))
finally (return nil)))
Testing in SBCL:
* (FIND-LAST-COMMA "hi, hi, ho")
debugger invoked on a SIMPLE-TYPE-ERROR in thread
#<THREAD "main thread" RUNNING {23EAC201}>:
Value of N in (AREF N S) is 9, not a ARRAY.
Gauche Scheme
(string-scan-right "hi, hi, ho" #\,)
===>
6
(string-scan-right "hi, hi, ho" #\, 'before)
===>
"hi, hi"
(string-scan-right "hi, hi, ho" #\, 'after)
===>
" ho"