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)))))
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))
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 ...)
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
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.
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
> 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
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
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
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
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.
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 ***
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...
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/