Sujet : Re: Macro noob
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 18. Jun 2025, 10:17:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <102u07q$31nf3$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Frank Buss wrote:
(defun count-words (string)
"returns the number of words in a string"
(loop for char across string
with count = 0 and in-word = nil
finally (return count)
do (if (alphanumericp char)
(unless in-word
(setf in-word t)
(incf count))
(setf in-word nil))))
Gauche Scheme:
(use srfi-13) ;; string-index string-skip
(use srfi-14) ;; character sets
(define (count-words str)
(define p 0) ;; Position.
(let go ((cnt 0) (inc-a 1) (inc-b 0)
(f0 string-index) (f1 string-skip))
(set! p (f0 str char-set:letter+digit p))
(if p
(go (+ cnt inc-a) inc-b inc-a f1 f0)
cnt)))
gosh> (count-words "hi ?? ho xx23zz")
3
gosh> (count-words " !! ")
0
gosh> (count-words " ")
0
gosh> (count-words " foo---bar ")
2