• Re: Illegal LOOP usage?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Wed Jun 18 21:57:34 2025
    From Newsgroup: comp.lang.lisp

    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,,,")
    --- Synchronet 3.21d-Linux NewsLink 1.2