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