Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: take a sequence of numbers 2 at a time, print numbers and their product?

44 views
Skip to first unread message

WJ

unread,
May 20, 2012, 9:32:41 AM5/20/12
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Lieven Marchand wrote:

> (Bob Felts) writes:
>
> > Kaz Kylheku <kkylh...@gmail.com> wrote:
> >
> >> On Dec 27, 2:34 pm, "livingcosmos.org" <metap...@gmail.com> wrote:
> >> > How would you take a sequence of numbers in groups of 2 and print out
> >> > the numbers and their product?
> >> >
> > [...]
> >>
> >> It's perhaps more useful to collect the results than print.
> >>
> >> State machine approach:
> >>
> >> (loop for x across nums
> >> with acc
> >> if (null acc) do
> >> (setf acc x)
> >> else
> >> collect (* x acc) and do
> >> (setf acc nil))
> >
> > Why not just:
> >
> > (loop
> > for x on nums by #'cddr
> > collect (* (first x) (second x)))
> >
> >
> > The original problem might then be:
> >
> > (loop
> > for x on nums by #'cddr
> > for a = (first x) and b = (second x)
> > do
> > (format t "~A * ~A = ~A~%" a b (* a b)))
>
> You can let loop do the destructuring:
>
> (loop for (a b nil) on nums by #'cddr ...)



Mark Tarver wrote:

> > The simple fact is that a loop form will
> > beat any other syntax hands down in terms of succinctness and
> > clarity
>
> In a modern FPL not always and not here.
>
> (define pp
> [X Y | Z] -> (do (output "~A x ~A = ~A~%" X Y (* X Y)) (pp Z))
> _ -> _)

Racket:

(define pp
(match-lambda
[(list x y z ...) (printf "~a x ~a = ~a~%" x y (* x y)) (pp z)]
[_ #t]))

WJ

unread,
Dec 5, 2012, 10:02:07 PM12/5/12
to
EMACS Lisp:

(require 'cl)

(defun* pp ((x y . tail))
(princ (format "%d x %d = %d\n" x y (* x y)))
(when tail (pp tail)))

(pp (number-sequence 0 21))

0 x 1 = 0
2 x 3 = 6
4 x 5 = 20
6 x 7 = 42
8 x 9 = 72
10 x 11 = 110
12 x 13 = 156
14 x 15 = 210
16 x 17 = 272
18 x 19 = 342
20 x 21 = 420

WJ

unread,
Dec 26, 2012, 11:56:57 PM12/26/12
to
newLISP:

(define (pp lst)
(when lst
(println (format "%d * %d = %d" (lst 0) (lst 1) (apply * (0 2 lst))))
(pp (2 lst))))

> (pp (sequence 1 22))
1 * 2 = 2
3 * 4 = 12
5 * 6 = 30
7 * 8 = 56
9 * 10 = 90
11 * 12 = 132
13 * 14 = 182
15 * 16 = 240
17 * 18 = 306
19 * 20 = 380
21 * 22 = 462

WJ

unread,
Apr 10, 2013, 9:24:05 PM4/10/13
to
Instead of CL, tCL.

namespace path {::tcl::mathop ::tcl::mathfunc}

foreach {x y} {2 3 4 5 6 7} {puts "$x x $y = [* $x $y]"}
0 new messages