From: livingcosmos.org
Subject: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <403a2cc2-3f9d-4e34-9066-cdcdf0ccc5af@i12g2000prf.googlegroups.com>
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))

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

From: Kaz Kylheku
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <d81dfe85-efe1-4471-9f1e-c0dbbb4c7085@w56g2000hsf.googlegroups.com>
On Dec 27, 2:34 pm, "livingcosmos.org" <········@gmail.com> 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))
>
> (loop for i from 0 below (car (array-dimensions nums)) by 2

What's wrong with (length nums)?

If the array to be multi-dimensional, your 1D arefs won't work anyway.

>      do (progn

The do clause already takes a list of forms; no progn required.

>           (let ((a (aref nums i))
>                 (b (aref nums (1+ i))))
>           (format t "~A * ~A = ~A~%" a b (* a b)))))

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))
From: Bob Felts
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <1i9stya.lwpidwt23se4N%wrf3@stablecross.com>
Kaz Kylheku <········@gmail.com> wrote:

> On Dec 27, 2:34 pm, "livingcosmos.org" <········@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)))
From: Lieven Marchand
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <87zlvvj8s6.fsf@wyrd.be>
····@stablecross.com (Bob Felts) writes:

> Kaz Kylheku <········@gmail.com> wrote:
>
>> On Dec 27, 2:34 pm, "livingcosmos.org" <········@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 ...)
From: Nicolas Neuss
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <87y7bec201.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Lieven Marchand <···@wyrd.be> writes:

> You can let loop do the destructuring:
>
> (loop for (a b nil) on nums by #'cddr ...)

And you can omit nil in this construct.

Nicolas
From: Bob Felts
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <1i9tw5f.1hboiqx1k7visaN%wrf3@stablecross.com>
Nicolas Neuss <········@math.uni-karlsruhe.de> wrote:

> Lieven Marchand <···@wyrd.be> writes:
> 
> > You can let loop do the destructuring:
> >
> > (loop for (a b nil) on nums by #'cddr ...)
> 
> And you can omit nil in this construct.
> 
> Nicolas

I actually tried this "(loop for (a b) on nums...)" but it didn't work
for me.  The "by #'cddr" is what I needed.  Thanks.
From: Mark Tarver
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <921596bd-738e-47f5-bcc8-9754b737b379@i29g2000prf.googlegroups.com>
On 27 Dec, 22:34, "livingcosmos.org" <········@gmail.com> 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))
>
> (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)))))

People might advise otherwise but I'd try and get away from the loop
construction; it delays your evolution into thinking like a functional
programmer.  Think lists and recursion.

Mark
From: Ken Tilton
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their  product?
Date: 
Message-ID: <477486ad$0$13896$607ed4bc@cv.net>
Mark Tarver wrote:
> On 27 Dec, 22:34, "livingcosmos.org" <········@gmail.com> 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))
>>
>>(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)))))
> 
> 
> People might advise otherwise but I'd try and get away from the loop
> construction; it delays your evolution into thinking like a functional
> programmer.  Think lists and recursion.

Pfft. We are trying to write applications, not placate some wrathful god 
of one paradigm or another. The simple fact is that a loop form will 
beat any other syntax hands down in terms of succinctness and clarity 
for any requirement other than those where (mapcar 'xxx yyy) or maphash 
requiring access to both keys and values will suffice. Lawdy loop is 
verbose iterating over both key and value of hash tables  -- what went 
wrong at /that/ design meeting?!

kt

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Mark Tarver
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <bbb64bc9-285a-4bdc-885f-9ce588387e03@y5g2000hsf.googlegroups.com>
> 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))
  _ -> _)

Mark
From: viper-2
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <a6319806-b9e4-4d37-b89b-2054e552f795@s27g2000hsb.googlegroups.com>
On Dec 28, 11:47 am, Mark Tarver <··········@ukonline.co.uk> 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))
>   _ -> _)
>
> Mark

Ever since we returned in that ball of light I've been playing catch
up, but I don't
recognize this language. Which FPL is it?

agt
From: John Thingstad
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their product?
Date: 
Message-ID: <op.t3118mx1ut4oq5@pandora.alfanett.no>
P� Fri, 28 Dec 2007 18:04:46 +0100, skrev viper-2  
<········@mail.infochan.com>:

> On Dec 28, 11:47 am, Mark Tarver <··········@ukonline.co.uk> 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))
>>   _ -> _)
>>
>> Mark
>
> Ever since we returned in that ball of light I've been playing catch
> up, but I don't
> recognize this language. Which FPL is it?
>
> agt

Qi written by Mark Tarver

http://www.lambdassociates.org/lC21.htm

--------------
John Thingstad
From: viper-2
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <a0767f17-c9a0-42cd-81e5-bd5bea42759f@f3g2000hsg.googlegroups.com>
On Dec 28, 12:15 pm, "John Thingstad" <·······@online.no> wrote:
> På Fri, 28 Dec 2007 18:04:46 +0100, skrev viper-2
> <········@mail.infochan.com>:

> > Ever since we returned in that ball of light I've been playing catch
> > up, but I don't
> > recognize this language. Which FPL is it?
>
> > agt
>
> Qi written by Mark Tarver
>
> http://www.lambdassociates.org/lC21.htm

I am truly impressed. I'll append Qi to my languages-to-learn list.

agt
From: viper-2
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <7b452a34-e0a1-4c01-b538-7b5272770a71@e25g2000prg.googlegroups.com>
On Dec 27, 6:57 pm, Mark Tarver <··········@ukonline.co.uk> wrote:

> People might advise otherwise but I'd try and get away from the loop
> construction; it delays your evolution into thinking like a functional
> programmer.  Think lists and recursion.
>
> Mark


Seconded! I'd rather catch up on Java and Python before delving
into that other language "loop" :-)


>
(setf nums (list 1 2 3 4 5 6 7 8))
(1 2 3 4 5 6 7 8)

>
(defun pair-printer (num-lst)
  (when num-lst
    (format t "~% ~a ~a ~a" (first num-lst) (second num-lst)
	    (* (first num-lst) (second num-lst)))
    (pair-printer (rest (rest num-lst)))))
PAIR-PRINTER

>
(pair-printer nums)
 1 2 2
 3 4 12
 5 6 30
 7 8 56
NIL

You may now add all the bells and whistles to cover odd
numbers of elements etc.

agt
From: Leandro Rios
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their  product?
Date: 
Message-ID: <47752c06$0$1348$834e42db@reader.greatnowhere.com>
viper-2 escribi�:
> On Dec 27, 6:57 pm, Mark Tarver <··········@ukonline.co.uk> wrote:
> 
>> People might advise otherwise but I'd try and get away from the loop
>> construction; it delays your evolution into thinking like a functional
>> programmer.  Think lists and recursion.
>>
>> Mark
> 
> 
> Seconded! I'd rather catch up on Java and Python before delving
> into that other language "loop" :-)
> 


In the same spirit, using a mapping function:

(defparameter *nums* '(21 47
		       23 43
		       24 29
		       22 28
		       22 53
		       31 36
		       22 56
		       31 72))

(mapc (let ((prev 0))
	#'(lambda (x)
	    (cond ((= prev 0) (setf prev x))
		  (t (format t "~A * ~A = ~A~%" prev x (* x prev))
		     (setf prev 0)))))
       *nums*)

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

Leandro
From: Paul Donnelly
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <pan.2007.12.28.01.37.46.802174@sbcglobal.net>
On Thu, 27 Dec 2007 14:34:28 -0800, livingcosmos.org 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))
> 
> (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)))))

Unless there's a good reason for all the numbers to be in the same list,
something like (mapcar print-fn set-1 set-2) would be the usual way to go
about this. Print-fn would just be a lambda taking two arguments
containing the format call you used.
From: Barry Margolin
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <barmar-CDF0E2.21015027122007@comcast.dca.giganews.com>
In article <······························@sbcglobal.net>,
 Paul Donnelly <·············@sbcglobal.net> wrote:

> On Thu, 27 Dec 2007 14:34:28 -0800, livingcosmos.org 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))
> > 
> > (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)))))
> 
> Unless there's a good reason for all the numbers to be in the same list,
> something like (mapcar print-fn set-1 set-2) would be the usual way to go
> about this. Print-fn would just be a lambda taking two arguments
> containing the format call you used.

But he said you're given a sequence of numbers, not two sequences of 
numbers.  Obviously this is a homework problem, and you don't get to 
change the problem to suit your esthetics.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Paul Donnelly
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their 	product?
Date: 
Message-ID: <pan.2007.12.28.08.00.14.652206@sbcglobal.net>
On Thu, 27 Dec 2007 21:01:50 -0500, Barry Margolin wrote:

> In article <······························@sbcglobal.net>,
>  Paul Donnelly <·············@sbcglobal.net> wrote:
> 
>> On Thu, 27 Dec 2007 14:34:28 -0800, livingcosmos.org wrote:
>> 
>> > How would you take a sequence of numbers in groups of 2 and print out
>> > the numbers and their product?
>>
>> Unless there's a good reason for all the numbers to be in the same list,
>> something like (mapcar print-fn set-1 set-2) would be the usual way to go
>> about this. Print-fn would just be a lambda taking two arguments
>> containing the format call you used.
> 
> But he said you're given a sequence of numbers, not two sequences of 
> numbers.  Obviously this is a homework problem, and you don't get to 
> change the problem to suit your esthetics.

Sorry, I forgot we're only allowed to answer the question exactly as asked. :(
From: Steven M. Haflich
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their  product?
Date: 
Message-ID: <vr1dj.1944$se5.383@nlpi069.nbdc.sbc.com>
cl-user(2): (defparameter *a* (vector 1 2 3 4 5 6 7 8 9 10))
*a*
cl-user(3): (loop for x across *a*
                 with y
                 when y
                 do (format t "~a * ~a = ~a~%" y x (* x y))
                    (setf y nil)
                 else do (setf y x))

This solution is cute but really really ugly.  I wouldn't turn
it in for homework...
From: Rainer Joswig
Subject: Re: take a sequence of numbers 2 at a time, print numbers and their product?
Date: 
Message-ID: <joswig-72B15C.00251128122007@news-europe.giganews.com>
In article 
<····································@i12g2000prf.googlegroups.com>,
 "livingcosmos.org" <········@gmail.com> 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)))

-- 
http://lispm.dyndns.org/