From: ············@gmail.com
Subject: Can you help me get rid of the setf?
Date: 
Message-ID: <1187285584.827170.30940@r29g2000hsg.googlegroups.com>
I'm looping over two variables to maximize a function, returning x and
y that maximize f.  Any way to do it without the setf in the body?

; Here's a function you can paste to the REPL.

(defun f (x y)
  (- (+ (* 9 x) (* 4 y))
     (* x x)
     (* y y))

; And here's what I'm trying to do:

(let ((best-x -1)
      (best-y -1)
      (max nil))
  (loop :for x :below 10 :do
        (loop :for y :below 10 :do
              (let ((n (f x
y)))
                (if (or (null max) (> n
max))
                    (setf best-x
x
                          best-y
y
                          max n)))))
  (values best-x best-y))

Thanks for any help,

Andrew

From: George Neuner
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <0b89c3h8ee6j8hf098edp1l6f06837qbn3@4ax.com>
On Thu, 16 Aug 2007 17:33:04 -0000, ·············@gmail.com"
<············@gmail.com> wrote:

>
>I'm looping over two variables to maximize a function, returning x and
>y that maximize f.  Any way to do it without the setf in the body?
>
>; Here's a function you can paste to the REPL.
>
>(defun f (x y)
>  (- (+ (* 9 x) (* 4 y))
>     (* x x)
>     (* y y))
>
>; And here's what I'm trying to do:
>
>(let ((best-x -1)
>      (best-y -1)
>      (max nil))
>  (loop :for x :below 10 :do
>        (loop :for y :below 10 :do
>              (let ((n (f x
>y)))
>                (if (or (null max) (> n
>max))
>                    (setf best-x
>x
>                          best-y
>y
>                          max n)))))
>  (values best-x best-y))
>
>Thanks for any help,

This recursive version computes the same answer as your loops - at
least for your example.  Large x or y might cause trouble if your Lisp
is not tail recursive.

(defun g ()
  (labels (
          (g1 (x y best best-x best-y)
              (let ((curr (f x y)))
                 (cond
                    ((= x 10)      (values best-x best-y))
                    ((= y 10)      (g1 (1+ x) 0 best best-x best-y))
                    ((> curr best) (g1 x (1+ y) curr x y))
                    (t             (g1 x (1+ y) best best-x best-y))
                    )))                
          )
     (g1 0 0 -1 -1 -1)))


George
--
for email reply remove "/" from address
From: ············@gmail.com
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <1187285740.606787.293620@22g2000hsm.googlegroups.com>
Let's try formatting that code better.  Damn you google groups!

(defun f (x y)
  (- (+ (* 9 x) (* 4 y))
     (* x x)
     (* y y))

; And here's what I'm trying to do:

(let ((best-x -1)
      (best-y -1)
      (max nil))
  (loop :for x :below 10 :do
        (loop :for y :below 10 :do
              (let ((n (f x y)))
                (if (or (null max) (> n max))
                    (setf best-x x
                          best-y y
                          max n)))))
   (values best-x best-y))

Thanks for any help,

Andrew
From: ·······@gmail.com
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <1187287651.076842.257460@g4g2000hsf.googlegroups.com>
On Aug 16, 1:35 pm, ·············@gmail.com" <············@gmail.com>
wrote:
> Let's try formatting that code better.  Damn you google groups!
>
> (defun f (x y)
>   (- (+ (* 9 x) (* 4 y))
>      (* x x)
>      (* y y))
>
> ; And here's what I'm trying to do:
>
> (let ((best-x -1)
>       (best-y -1)
>       (max nil))
>   (loop :for x :below 10 :do
>         (loop :for y :below 10 :do
>               (let ((n (f x y)))
>                 (if (or (null max) (> n max))
>                     (setf best-x x
>                           best-y y
>                           max n)))))
>    (values best-x best-y))
>
> Thanks for any help,
>
> Andrew

This is recursive, but not in a pretty way...

(defun best-1-dimension (f max-num &optional (x 0) (y 0))
  (if (= max-num x) (funcall f x y)
    (max (funcall f x y)
         (best-1-dimension f max-num (1+ x) y))))

(defun best-2-dimensions (f max-num-x max-num-y &optional (x 0) (y 0))
  (if (= max-num-y y)
      (best-1-dimension f max-num-x 0 y)
    (max (best-1-dimension f max-num-x 0 y)
         (best-2-dimensions f max-num-x max-num-y 0 (1+ y)))))

(best-2-dimensions #'f 10 10)
24
From: Wade Humeniuk
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <m23aygrh09.fsf@telus.net.no.spam>
·············@gmail.com" <············@gmail.com> writes:

> Let's try formatting that code better.  Damn you google groups!
>
> (defun f (x y)
>   (- (+ (* 9 x) (* 4 y))
>      (* x x)
>      (* y y))
>
> ; And here's what I'm trying to do:
>
> (let ((best-x -1)
>       (best-y -1)
>       (max nil))
>   (loop :for x :below 10 :do
>         (loop :for y :below 10 :do
>               (let ((n (f x y)))
>                 (if (or (null max) (> n max))
>                     (setf best-x x
>                           best-y y
>                           max n)))))
>    (values best-x best-y))
>

Nothing like a little Lisp problem to get you going in the morning.

(defun f (x y)
  (- (+ (* 9 x) (* 4 y))
     (* x x)
     (* y y)))

(defun best-of-f ()
  (first (sort (loop :for x :below 10
		  nconc (loop :for y :below 10
			   collect (list x y (f x y))))
	       #'>
	       :key #'third)))

CL-USER> (best-of-f)
(4 2 24)
CL-USER> 
From: Ken Tilton
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <Pd6xi.74$YA2.56@newsfe12.lga>
············@gmail.com wrote:
> I'm looping over two variables to maximize a function, returning x and
> y that maximize f.  Any way to do it without the setf in the body?

Aw, jeez, I guess there is no one more fanatical than the new convert, 
but as Frode intimated, you have missed the point: as Kenny might say, 
what part of multi-paradigm language do you not understand?

That said, eliminating LET and SETF and anything else PG wanted to tax 
is a fun way to magically make ones code better. Just don't get, well, 
fanatical about it.

meanwhile, I /do/ think loop needs:

     maximize x into outer-best-x and y into outer-best-y and...

I need it about once a month, that should be good enough for the ANSI 
committee.

kt

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

"Algebra is the metaphysics of arithmetic." - John Ray

"As long as algebra is taught in school,
there will be prayer in school." - Cokie Roberts

"Stand firm in your refusal to remain conscious during algebra."
    - Fran Lebowitz

"I'm an algebra liar. I figure two good lies make a positive."
    - Tim Allen
From: Pillsy
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <1187312458.703278.216110@19g2000hsx.googlegroups.com>
On Aug 16, 8:46 pm, Ken Tilton <···········@optonline.net> wrote:
[...]
> meanwhile, I /do/ think loop needs:

>      maximize x into outer-best-x and y into outer-best-y and...

> I need it about once a month, that should be good enough for the ANSI
> committee.

That one's in ITERATE, if you're at all interested in checking it out.

I need it rather more than once a month, since I do a lot with
multidimensional arrays.

Cheers,
Pillsy
From: Ken Tilton
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <rraxi.182$YA2.122@newsfe12.lga>
Pillsy wrote:
> On Aug 16, 8:46 pm, Ken Tilton <···········@optonline.net> wrote:
> [...]
> 
>>meanwhile, I /do/ think loop needs:
> 
> 
>>     maximize x into outer-best-x and y into outer-best-y and...
> 
> 
>>I need it about once a month, that should be good enough for the ANSI
>>committee.
> 
> 
> That one's in ITERATE, if you're at all interested in checking it out.

It took me ten years to consider LOOP, see you in 2016.

kenzo
From: John Thingstad
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <op.tw7efpskpqzri1@pandora.upc.no>
P� Fri, 17 Aug 2007 07:34:13 +0200, skrev Ken Tilton  
<···········@optonline.net>:

>
>
> Pillsy wrote:
>> On Aug 16, 8:46 pm, Ken Tilton <···········@optonline.net> wrote:
>> [...]
>>
>>> meanwhile, I /do/ think loop needs:
>>
>>>     maximize x into outer-best-x and y into outer-best-y and...
>>
>>> I need it about once a month, that should be good enough for the ANSI
>>> committee.
>>   That one's in ITERATE, if you're at all interested in checking it out.
>
> It took me ten years to consider LOOP, see you in 2016.
>
> kenzo

iterate roughly follows the same structure and naming as loop.
If you know loop (and you do) you should not have a problem with iterate.

Example:

(loop repeat 100 collect (random 100))

(iter (repeat 100) (collect (random 100)))

(loop for i from 1 to 100 do (print i))

(iter (for i from 1 to 100) (print i))

But there are more features like generators.
An because it is made by a collection of macroes it is easelly extensible.
From: ············@gmail.com
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <1187321342.756523.234030@19g2000hsx.googlegroups.com>
Hey, that iterate's not bad.

(iter (for x to 10)
      (for (values y f)
           = (iter (for y to 10)
                   (finding y maximizing (f x y) into (best-y best-f))
                   (finally (return (values best-y best-f)))))
           (finding x maximizing f into best-x)
           ; it hurts a little to write this same clause twice.
alternatives?
           (finding y maximizing f into best-y)
           (finally (return (values best-x best-y))))

It would be nice to write (finding x and y maximizing f into best-x
and best-y) like you were saying Ken.  Oh and I know, I know, I'm
being overzealous with the setfs.

Thanks to all.

Andrew

p.s.  sorry in advance because I know this code will be formatted
poorly upon posting.
From: Rob Warnock
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <N76dnQpjGaDujVjbnZ2dnUVZ_vShnZ2d@speakeasy.net>
Ken Tilton  <·········@gmail.com> wrote:
+---------------
| ············@gmail.com wrote:
| > I'm looping over two variables to maximize a function, returning x and
| > y that maximize f.  Any way to do it without the setf in the body?
...
| meanwhile, I /do/ think loop needs:
| 
|      maximize x into outer-best-x and y into outer-best-y and...
| 
| I need it about once a month, that should be good enough for the ANSI 
| committee.
+---------------

I do agree that LOOP's lack of nestable lexical scopes is a pain,
but waiting for ANSI is like... What was that guy's name...? Godot?

While you're waiting, CMUCL has COLLECT, the ITERATE package has,
well, ITERATE, and even plain ol' ANSI CL LOOP can be coerced
(screaming & kicking) into doing *something* along these lines,
albeit plug-ugly:

    > (loop for i below 10
	    for (tmp-x . tmp-y) =
		(loop for j below 10
		      for x = (+ (* i i) (* j j))
		      and y = (- (* i i) (* j j))
		   maximize x into inner-best-x       
		   maximize y into inner-best-y
		   finally (return (cons inner-best-x inner-best-y)))
	maximize tmp-x into outer-best-x
	maximize tmp-y into outer-best-y        
	finally (return (cons outer-best-x outer-best-y)))        

    (162 . 81)
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Frode Vatvedt Fjeld
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <2hlkcbxnoq.fsf@vserver.cs.uit.no>
 ·············@gmail.com" <············@gmail.com> writes:

> I'm looping over two variables to maximize a function, returning x
> and y that maximize f.  Any way to do it without the setf in the
> body?

I don't think there's any substantially better way to do this than
what you posted. Why would you want to do this without setf?

-- 
Frode Vatvedt Fjeld
From: Frank Buss
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <hfoakc8yqd9o.4qjg7bashgd1.dlg@40tude.net>
············@gmail.com wrote:

> I'm looping over two variables to maximize a function, returning x and
> y that maximize f.  Any way to do it without the setf in the body?

Looks like you are searching for a functional way of doing this. In Haskell
you could write it like this (I'm a Haskell newbie, I'm sure this can be
written better, but at least it works)

f (x,y) = 9*x + 4*y - x*x - y*y
best = foldr1 max values
    where values = [(f(x,y), (x,y)) | x<-[0..9], y<-[0..9]]
          max t1 t2 = if (fst t1) >= (fst t2) then t1 else t2

This gives (24,(4,2)). You can write "snd $ foldr1 max values", if you want
just the function arguments and not the max value.

In Lisp you can do the same:

(defun best ()
  (let ((max (lambda (x y)
               (if (>= (first x) (first y)) x y)))
        (values (reduce #'nconc
                       (loop for x below 10 collect
                             (loop for y below 10 collect
                                   `(,(f x y) (,x ,y)))))))
    (reduce max values)))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Paul Rubin
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <7x3ayid4kk.fsf@ruckus.brouhaha.com>
Frank Buss <··@frank-buss.de> writes:
> Looks like you are searching for a functional way of doing this. In Haskell
> you could write it like this (I'm a Haskell newbie, I'm sure this can be
> written better, but at least it works)
> 
> f (x,y) = 9*x + 4*y - x*x - y*y
> best = foldr1 max values
>     where values = [(f(x,y), (x,y)) | x<-[0..9], y<-[0..9]]
>           max t1 t2 = if (fst t1) >= (fst t2) then t1 else t2

  let f x y = 9*x+4*y-x*x-y*y in 
     foldl1' max [(f x y,(x,y)) | x <- [0..9], y<-[0..9]]

is a little simpler.  Uses the max built-in and foldl1' which operates
left to right and is strict.
From: Rainer Joswig
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <joswig-D99CD1.09302017082007@news-europe.giganews.com>
In article <·····························@40tude.net>,
 Frank Buss <··@frank-buss.de> wrote:

> ············@gmail.com wrote:
> 
> > I'm looping over two variables to maximize a function, returning x and
> > y that maximize f.  Any way to do it without the setf in the body?
> 
> Looks like you are searching for a functional way of doing this. In Haskell
> you could write it like this (I'm a Haskell newbie, I'm sure this can be
> written better, but at least it works)
> 
> f (x,y) = 9*x + 4*y - x*x - y*y
> best = foldr1 max values
>     where values = [(f(x,y), (x,y)) | x<-[0..9], y<-[0..9]]
>           max t1 t2 = if (fst t1) >= (fst t2) then t1 else t2
> 
> This gives (24,(4,2)). You can write "snd $ foldr1 max values", if you want
> just the function arguments and not the max value.
> 
> In Lisp you can do the same:
> 
> (defun best ()
>   (let ((max (lambda (x y)
>                (if (>= (first x) (first y)) x y)))
>         (values (reduce #'nconc
>                        (loop for x below 10 collect
>                              (loop for y below 10 collect
>                                    `(,(f x y) (,x ,y)))))))
>     (reduce max values)))

You can remove the REDUCE #'NCONC form. LOOP
allows NCONC - which you can use instead of COLLECT and
the REDUCE call in this case.

-- 
http://lispm.dyndns.org
From: Frank Buss
Subject: Re: Can you help me get rid of the setf?
Date: 
Message-ID: <1tdoi5frcvj5g.r10a124virek$.dlg@40tude.net>
Rainer Joswig wrote:

> You can remove the REDUCE #'NCONC form. LOOP
> allows NCONC - which you can use instead of COLLECT and
> the REDUCE call in this case.

Thanks, then it looks a bit better:

(defun best ()
  (let ((max (lambda (x y)
               (if (>= (first x) (first y)) x y)))
        (values (loop for x below 10 nconc
                      (loop for y below 10 collect
                            `(,(f x y) (,x ,y))))))
    (reduce max values)))

But I think the Haskell version has the advantage that it doesn't need to
build the list in memory, because it has lazy evaluation.

Another nice idea is that you can compare lists and tuples with <, > etc.
in Haskell, if the items are instances of Ord (orderable) (at least with
GHC), which means that "max" works with lists as well. Is it possible in
Lisp to define an Ord system class and other system classes, which has Ord
and another system class as predecessors, e.g. List could be a Sequence and
an Ord? But I'm not sure if this makes sense, because lists could contain
items which doesn't have a natural order, like complex numbers.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de