Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

English literature's performing flea. -- Sean O'Casey on P. G. Wodehouse


comp / comp.lang.scheme / Re: Better way to write this function?

SubjectAuthor
* Re: Better way to write this function?B. Pym
`- Re: Better way to write this function?B. Pym

1
Subject: Re: Better way to write this function?
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Tue, 10 Sep 2024 06:37 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Better way to write this function?
Date: Tue, 10 Sep 2024 06:37:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <vbopfd$2qrpe$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 10 Sep 2024 08:37:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c4bf57cd3b1fb81e9c3e991dcd41f255";
logging-data="2977582"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+I3XNSQQEg1MSqSjTMzTNT"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:tMhQ+DD+SER9eJrP70OA50Jq/iw=
View all headers

Kenny Tilton wrote:

> Peter Seibel wrote:
> > As part of a larger program I needed a function which given a number
> > (n) and a vector (row), returns the index into row of the n'th nil in
> > row. Here's the first (working) version I came up with:
> >
> > (defun nth-slot (n v)
> > "Find the nth nil cell in a vector v."
> > (loop with slots-seen = 0
> > for item across v
> > for idx from 0
> > counting (not item) into slots-seen
> > until (> slots-seen n)
> > finally (if (> slots-seen n) (return idx) (error "No slot"))))
>
> Please take that inscrutal glop to comp.lang.loop. <g>
>
> (defun nth-null-slot-index (n v &optional must-find-p &aux (nil-ct 0))
> "Return the index of the nth nil cell in vector v"
> (dotimes (x (length v) (when must-find-p (error "bzzzt")))
> (when (null (elt v x))
> (when (= n (incf nil-ct)) ;; [1]
> (return-from nth-null-slot-index x)))))
>
> (nth-null-slot-index 3 #(nil 1 2 nil 3 4 nil 5 nil nil))
> =>6
>

The first nil found is considered to be number 1,
not number 0.

Gauche Scheme

(use gauche.generator)

;; "We don't need no stinkin' loops!"
(define (nth-null-slot-index n vec)
(list-ref
(generator->list
;; Gauche coerces the vector to a generator.
(gselect (grange 0) (gmap not vec)))
(- n 1)))

(nth-null-slot-index 3 #(#f a b #f c d #f e #f #f))
===>
6

Subject: Re: Better way to write this function?
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Tue, 10 Sep 2024 20:17 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Better way to write this function?
Date: Tue, 10 Sep 2024 20:17:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <vbq9gi$351s7$1@dont-email.me>
References: <vbopfd$2qrpe$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 10 Sep 2024 22:17:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="44e4ceff29e8606dec644044dccf39f6";
logging-data="3311495"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+p+GZ9hdy6b+XcQTKbAR4c"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:xlRpff3ILpfteMtmtTpmldYtGec=
View all headers

B. Pym wrote:

> Kenny Tilton wrote:
>
> > Peter Seibel wrote:
> > > As part of a larger program I needed a function which given a number
> > > (n) and a vector (row), returns the index into row of the n'th nil in
> > > row. Here's the first (working) version I came up with:
> > >
> > > (defun nth-slot (n v)
> > > "Find the nth nil cell in a vector v."
> > > (loop with slots-seen = 0
> > > for item across v
> > > for idx from 0
> > > counting (not item) into slots-seen
> > > until (> slots-seen n)
> > > finally (if (> slots-seen n) (return idx) (error "No slot"))))
> >
> > Please take that inscrutal glop to comp.lang.loop. <g>
> >
> > (defun nth-null-slot-index (n v &optional must-find-p &aux (nil-ct 0))
> > "Return the index of the nth nil cell in vector v"
> > (dotimes (x (length v) (when must-find-p (error "bzzzt")))
> > (when (null (elt v x))
> > (when (= n (incf nil-ct)) ;; [1]
> > (return-from nth-null-slot-index x)))))
> >
> > (nth-null-slot-index 3 #(nil 1 2 nil 3 4 nil 5 nil nil))
> > =>6
> >
>
> The first nil found is considered to be number 1,
> not number 0.
>
> Gauche Scheme
>
> (use gauche.generator)
>
> ;; "We don't need no stinkin' loops!"
> (define (nth-null-slot-index n vec)
> (list-ref
> (generator->list
> ;; Gauche coerces the vector to a generator.
> (gselect (grange 0) (gmap not vec)))
> (- n 1)))
>
>
> (nth-null-slot-index 3 #(#f a b #f c d #f e #f #f))
> ===>
> 6

Without using generators:

(define (nth-null-slot-index n vec)
(list-ref
(filter-map
(^(a b) (and b a))
(lrange 0)
(map not (vector->list vec)))
(- n 1)))

1

rocksolid light 0.9.8
clearnet tor