Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

(It is an old Debian tradition to leave at least twice a year ...) -- Sven Rudolph


devel / comp.lang.lisp / .Re: take a sequence of numbers 2 at a time, print numbers and

SubjectAuthor
o .Re: take a sequence of numbers 2 at a time, print numbers andRobert L.

1
.Re: take a sequence of numbers 2 at a time, print numbers and

<t0u4o0$1a9n$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!FJv/bGwhGSz/196zv+MMkA.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: take a sequence of numbers 2 at a time, print numbers and
Date: Thu, 17 Mar 2022 02:00:36 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t0u4o0$1a9n$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="43319"; posting-host="FJv/bGwhGSz/196zv+MMkA.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 Mar 2022 02:00 UTC

Rainer Joswig wrote:

> > How would you take a sequence of numbers in groups of 2 and print out
> > the numbers and their product?
> >
> > Here is my solution, but I'm sure some li5p h4x0r has a much more 1337
> > solution :)
> >
> > (setf nums (vector 21 47
> > 23 43
> > 24 29
> > 22 28
> > 22 53
> > 31 36
> > 22 56
> > 31 72))
>
> Remember that variables are introduced with DEFPARAMETER (or DEFVAR)
> and not with SETF.
>
> Also remember that all top-level variables should be by convention
> written like this: *nums*
>
> >
> > (loop for i from 0 below (car (array-dimensions nums)) by 2
> > do (progn
> > (let ((a (aref nums i))
> > (b (aref nums (1+ i))))
> > (format t "~A * ~A = ~A~%" a b (* a b)))))
>
> ARRAY-DIMENSION is not the best operation for a vector in
> general, since the vector could be one with a fill-pointer.
> LENGTH will give you the real used length of a vector.
>
> This solution is basically the same as yours but a bit
> differently written:
>
> (loop with size = (length *nums*)
> for i from 0 below size by 2
> for j from 1 below size by 2
> for a = (aref *nums* i) and b = (aref *nums* j)
> do (format t "~A * ~A = ~A~%" a b (* a b)))

Shorter.

Gauche Scheme or Racket

(use srfi-42) ;; do-ec for Gauche
or
(require srfi/42) ;; do-ec for Racket
(require srfi/43) ;; vector->list for Racket
(require srfi/48) ;; format for Racket

(define nums
(vector 21 47
23 43
24 29
22 28
22 53
31 36
22 56
31 72))

(do-ec (:range i 0 (vector-length nums) 2)
(:let both (vector->list nums i (+ i 2)))
(apply format #t "~a * ~a = ~a~%"
(append both (list (apply * both)))))
21 * 47 = 987
23 * 43 = 989
24 * 29 = 696
22 * 28 = 616
22 * 53 = 1166
31 * 36 = 1116
22 * 56 = 1232
31 * 72 = 2232


devel / comp.lang.lisp / .Re: take a sequence of numbers 2 at a time, print numbers and

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor