[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Can you help me get rid of the setf?

William James

12/27/2015 8:16:00 AM

andrew.baine 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))

MatzLisp (Ruby):

def f x,y; 9*x+4*y-x*x-y*y; end

a=(0..9).to_a
a.product(a).max_by{|x,y| f x,y}
===>
[4, 2]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...