| Deutsch English Français Italiano |
|
<102uukd$393s0$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Re: Macro noob
Date: Wed, 18 Jun 2025 17:56:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <102uukd$393s0$1@dont-email.me>
References: <102u07q$31nf3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 18 Jun 2025 19:56:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1d8279f8f1ebce16497eecb88ef68b8e";
logging-data="3444608"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19V04QdZ4Jr8szAJhbaJXQV"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:hbfKXu9bTnvIzUFM/vjfAFnzOUU=
B. Pym wrote:
> 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
More understanbable.
(define (count-words str)
(define p 0) ;; Position.
(let go ((cnt 0)
(inc-a 1) ;; Amount to increment cnt when finding alphanumerics.
(inc-b 0) ;; Amount to increment cnt when skipping alphanumerics.
(func-a string-index) ;; Function to find an alphanumeric.
(func-b string-skip)) ;; Function to skip alphanumerics.
(set! p (func-a str char-set:letter+digit p))
(if p
;; When we recurse, we swap the increments and the
;; functions. So if we were finding alphanumerics,
;; during the next pass we'll be skipping alphanumerics.
(go (+ cnt inc-a) inc-b inc-a func-b func-a)
cnt)))