• take a sequence of numbers 2 at a time, print numbers and

    From Robert L.@21:1/5 to Rainer Joswig on Tue Feb 15 09:41:46 2022
    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)))

    (use srfi-43) ;; vector->list for Gauche
    or
    (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 ((i 0 (+ i 2)))
    ((= i (vector-length nums)))
    (let ((both (vector->list nums i (+ i 2))))
    (apply format #t "~a * ~a = ~a~%" `(,@both ,(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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Rainer Joswig on Wed Aug 6 04:00:17 2025
    XPost: comp.lang.scheme

    Rainer Joswig wrote:

    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)))

    Gauche Scheme

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

    (with-gen (n nums)
    (until (n-null?)
    (let ((a (n)) (b (n)))
    (format #t "~a * ~a = ~a\n" a b (* a b)))))

    21 * 47 = 987
    23 * 43 = 989
    24 * 29 = 696
    22 * 28 = 616
    22 * 53 = 1166
    31 * 36 = 1116
    22 * 56 = 1232
    31 * 72 = 2232

    Given:

    ;; Make a vector generator.
    (define (gmk vec)
    (let ((v vec)
    (i 0))
    (values
    (lambda()
    (if (= i (vector-length v)) #f (begin0 (~ v i) (inc! i))))
    ;; Tells if generator is exhausted.
    (lambda() (= i (vector-length v))))))

    (define-macro with-gen
    (lambda (args . body)
    (define duos
    (let go ()
    (if (null? args) '() (cons (list (pop! args)(pop! args)) (go)))))
    (define (proc s v)
    `((,s ,(symbol-append s '-null?)) (gmk ,v)))
    `(let-values
    (,@(map (lambda (xs) (apply proc xs)) duos))
    ,@body)))


    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)