Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

An Ada exception is when a routine gets in trouble and says 'Beam me up, Scotty'.


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

SubjectAuthor
o .Re: Better way to write this function?Robert L.

1
.Re: Better way to write this function?

<suknra$1mi2$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16875&group=comp.lang.lisp#16875

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!oLbwv41N8DscUwPT6F1sxQ.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: Better way to write this function?
Date: Thu, 17 Feb 2022 05:52:44 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <suknra$1mi2$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="55874"; posting-host="oLbwv41N8DscUwPT6F1sxQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Thu, 17 Feb 2022 05:52 UTC

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
>

We all tend to forget how powerful DO is.

Scheme:

(define (nth-false-index n vec)
(do ((i 0 (+ 1 i))
(n n (if (vector-ref vec i) n (- n 1))))
((or (zero? n) (and (= i (vector-length vec)) (error 'bzzt)))
(- i 1))))

(nth-false-index 1 #(#f 1 2 #f 3 4 #f 5 #f #f))
===>
0

(nth-false-index 2 #(#f 1 2 #f 3 4 #f 5 #f #f))
===>
3

(nth-false-index 3 #(#f 1 2 #f 3 4 #f 5 #f #f))
===>
6

(nth-false-index 4 #(#f 1 2 #f 3 4 #f 5 #f #f))
===>
8

(nth-false-index 1 #(2 3 4 5 ))
===>
*** ERROR: bzzt

(nth-false-index 2 #(2 3 4 5 #f))
===>
*** ERROR: bzzt


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor