Sujet : Re: Illegal LOOP usage?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 18. Jun 2025, 22:57:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <102vcoa$3dbeg$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Edi Weitz wrote:
So, this function from Drakma
(defun split-string (string &optional (separators " ,-"))
"Splits STRING into substrings separated by the characters in the
sequence SEPARATORS. Empty substrings aren't collected."
(loop for char across string
when (find char separators :test #'char=)
when collector
collect (coerce collector 'string) into result
and do (setq collector nil) end
else
collect char into collector
finally (return (if collector
(append result (list (coerce collector 'string)))
result))))
works as intended with LispWorks, but doesn't work with AllegroCL or
SBCL. It seems the latter two simply ignore the (SETQ COLLECTOR NIL)
form. My guess is that the code above is somehow incorrect in that it
modifies a variable which is used in a "collect ... into ..." clause,
but I couldn't find anything in the CLHS that explicitly says so.
Any hints?
Gauche Scheme
(use srfi-13) ;; string-tokenize
(use srfi-14) ;; character sets
(define (split-string strng :optional (separators " ,-"))
(define good-chars
(char-set-complement (string->char-set separators)))
(string-tokenize strng good-chars))
(split-string " foo bar-baz--zoo,,sooth,,,")
===>
("foo" "bar" "baz" "zoo" "sooth")
(split-string " foo bar-baz--zoo,,sooth,,," " o")
===>
("f" "bar-baz--z" ",,s" "th,,,")