Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

There will be big changes for you but you will be happy.


comp / comp.lang.lisp / Re: collect for vector

SubjectAuthor
o Re: collect for vectorB. Pym

1
Subject: Re: collect for vector
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 20 Jul 2024 09:22 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
Subject: Re: collect for vector
Date: Sat, 20 Jul 2024 09:22:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <v7fvkm$3fnga$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 20 Jul 2024 11:22:34 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ccef5f04218b39d638a08dde3da2f478";
logging-data="3661322"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+j7lu/Adk8yk6cU1BkkiON"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:5ow7g9r8Y+6LCNtHmCUksgCoL58=
View all headers

Thomas A. Russ wrote:

> > (loop with x = (make array 6 :fill-pointer 0)
> > for i from 0 to 10 by 2 do (vector-push i x)
> > finally return x)
>
> Actually, the finally clause needs to be (return x) instead.
>
> But if you already assume you know the size of the final array, you can
> dispense with the fill pointer and extension and just set it directly:
>
> (loop with x = (make array 6)
> for val from 0 to 10 by 2
> as index upfrom 0
> do (setf (aref x index) val)
> finally return x)

Testing in SBCL:

* (loop with x = (make array 6)
for val from 0 to 10 by 2
as index upfrom 0
do (setf (aref x index) val)
finally return x)

debugger invoked on a SB-INT:SIMPLE-PROGRAM-ERROR in thread
#<THREAD "main thread" RUNNING {23EAC1B9}>:
A compound form was expected, but RETURN found.
current LOOP context: FINALLY RETURN X.

Gauche Scheme

(list->vector (iota 6 0 2))
===>
#(0 2 4 6 8 10)

Another way.

(vector-tabulate 6 (cut * 2 <>))

1

rocksolid light 0.9.8
clearnet tor