[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Am I missing something about (loop ... maximizing ...) ?

William James

4/6/2015 7:59:00 PM

Wade Humeniuk wrote:

> > | I came right back here when I realized that
> > | (defun find-maximizing-item (list fun)
> > | (reduce #'max list :key fun))
> > | was what I was originally looking for, but I see you beat me to it.
> > +---------------
> >
> > Except that only tells you what the maximum function value was,
> > not which item produced it! [...which is what I thought the original
> > question was.] That is, with this version:
> >
> > > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> >
> > 49
> > >
> >
> > doesn't tell you it was the "-7" that caused this result.
> >
>
> Which is remedied by,
>
> (defun find-maximizing-item (list fun)
> (values-list
> (reduce (lambda (e1 e2) (if (> (car e1) (car e2)) e1 e2))
> list :key (lambda (elt) (list (funcall fun elt) elt)))))
>
> CL-USER 4 > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> 49
> -7

Gauche Scheme:

(use srfi-42)

(define (find-maximizing-item coll func)
(apply values
(fold3-ec '() (: x coll)
(list (func x) x) identity
(^(a b) (if (< (car a) (car b)) b a)))))

(find-maximizing-item '(3 -4 2 5 -7 1 2) square)
49
-7

Also handles vectors:

(find-maximizing-item #(3 -4 2 5 -7 1 2) square)
49
-7

Also handles string:

(find-maximizing-item "ABCD" (^c (abs (- (x->integer c) 68))))
3
#\A


A version for Racket:

(require srfi/42)

(define (find-maximizing-item coll func)
(apply values
(fold3-ec '() (: x coll)
(list (func x) x) identity
(lambda (a b) (if (< (car a) (car b)) b a)))))

(find-maximizing-item '(3 -4 2 5 -7 1 2) sqr)
49
-7

5 Answers

William James

12/4/2015 4:29:00 AM

0

WJ wrote:

> Wade Humeniuk wrote:
>
> > > > I came right back here when I realized that
> > > > (defun find-maximizing-item (list fun)
> > > > (reduce #'max list :key fun))
> > > > was what I was originally looking for, but I see you beat me to it.
> > > +---------------
> > >
> > > Except that only tells you what the maximum function value was,
> > > not which item produced it! [...which is what I thought the original
> > > question was.] That is, with this version:
> > >
> > > > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > >
> > > 49
> > > >
> > >
> > > doesn't tell you it was the "-7" that caused this result.
> > >
> >
> > Which is remedied by,
> >
> > (defun find-maximizing-item (list fun)
> > (values-list
> > (reduce (lambda (e1 e2) (if (> (car e1) (car e2)) e1 e2))
> > list :key (lambda (elt) (list (funcall fun elt) elt)))))
> >
> > CL-USER 4 > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > 49
> > -7

Ocaml:

let find_maximizing_item func list =
let rec loop best = function
[] -> best
| x::xs -> loop (max (func x, x) best) xs
in loop (func (hd list), hd list) (tl list) ;;


find_maximizing_item (fun x -> x*x) [3;-4;2;5;-7;1;2];;
===>
(49, -7)

--
bigot, n. One who is obstinately and zealously attached to an
opinion that you do not entertain.
--- Ambrose Bierce

William James

12/4/2015 4:41:00 AM

0

WJ wrote:

> WJ wrote:
>
> > Wade Humeniuk wrote:
> >
> > > > > I came right back here when I realized that
> > > > > (defun find-maximizing-item (list fun)
> > > > > (reduce #'max list :key fun))
> > > > > was what I was originally looking for, but I see you beat me to it.
> > > > +---------------
> > > >
> > > > Except that only tells you what the maximum function value was,
> > > > not which item produced it! [...which is what I thought the original
> > > > question was.] That is, with this version:
> > > >
> > > > > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > >
> > > > 49
> > > > >
> > > >
> > > > doesn't tell you it was the "-7" that caused this result.
> > > >
> > >
> > > Which is remedied by,
> > >
> > > (defun find-maximizing-item (list fun)
> > > (values-list
> > > (reduce (lambda (e1 e2) (if (> (car e1) (car e2)) e1 e2))
> > > list :key (lambda (elt) (list (funcall fun elt) elt)))))
> > >
> > > CL-USER 4 > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > 49
> > > -7
>
> Ocaml:
>


open List;;



> let find_maximizing_item func list =
> let rec loop best = function
> [] -> best
> | x::xs -> loop (max (func x, x) best) xs
> in loop (func (hd list), hd list) (tl list) ;;
>
>
> find_maximizing_item (fun x -> x*x) [3;-4;2;5;-7;1;2];;
> ===>
> (49, -7)


William James

12/4/2015 4:48:00 AM

0

WJ wrote:

> WJ wrote:
>
> > WJ wrote:
> >
> > > Wade Humeniuk wrote:
> > >
> > > > > > I came right back here when I realized that
> > > > > > (defun find-maximizing-item (list fun)
> > > > > > (reduce #'max list :key fun))
> > > > > > was what I was originally looking for, but I see you beat me to it.
> > > > > +---------------
> > > > >
> > > > > Except that only tells you what the maximum function value was,
> > > > > not which item produced it! [...which is what I thought the original
> > > > > question was.] That is, with this version:
> > > > >
> > > > > > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > > >
> > > > > 49
> > > > > >
> > > > >
> > > > > doesn't tell you it was the "-7" that caused this result.
> > > > >
> > > >
> > > > Which is remedied by,
> > > >
> > > > (defun find-maximizing-item (list fun)
> > > > (values-list
> > > > (reduce (lambda (e1 e2) (if (> (car e1) (car e2)) e1 e2))
> > > > list :key (lambda (elt) (list (funcall fun elt) elt)))))
> > > >
> > > > CL-USER 4 > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > > 49
> > > > -7
> >
> > Ocaml:
> >
>
>
> open List;;
>
>
>
> > let find_maximizing_item func list =
> > let rec loop best = function
> > [] -> best
> > | x::xs -> loop (max (func x, x) best) xs
> > in loop (func (hd list), hd list) (tl list) ;;
> >
> >
> > find_maximizing_item (fun x -> x*x) [3;-4;2;5;-7;1;2];;
> > ===>
> > (49, -7)

Also works with floats and strings:

# find_maximizing_item (fun x -> x*.x) [3.;-4.;2.;5.;-7.;1.;2.];;
- : float * float = (49., -7.)
# find_maximizing_item String.lowercase ["cow";"ZOO";"how"];;
- : string * string = ("zoo", "ZOO")


Paul Rubin

12/4/2015 4:57:00 AM

0

"WJ" <w_a_x_man@yahoo.com> writes:
> let find_maximizing_item func list =
> let rec loop best = function
> [] -> best
> | x::xs -> loop (max (func x, x) best) xs
> in loop (func (hd list), hd list) (tl list) ;;

Sheesh, you are making Ocaml look bad.

William James

12/4/2015 6:41:00 AM

0

WJ wrote:

> WJ wrote:
>
> > WJ wrote:
> >
> > > Wade Humeniuk wrote:
> > >
> > > > > > I came right back here when I realized that
> > > > > > (defun find-maximizing-item (list fun)
> > > > > > (reduce #'max list :key fun))
> > > > > > was what I was originally looking for, but I see you beat me to it.
> > > > > +---------------
> > > > >
> > > > > Except that only tells you what the maximum function value was,
> > > > > not which item produced it! [...which is what I thought the original
> > > > > question was.] That is, with this version:
> > > > >
> > > > > > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > > >
> > > > > 49
> > > > > >
> > > > >
> > > > > doesn't tell you it was the "-7" that caused this result.
> > > > >
> > > >
> > > > Which is remedied by,
> > > >
> > > > (defun find-maximizing-item (list fun)
> > > > (values-list
> > > > (reduce (lambda (e1 e2) (if (> (car e1) (car e2)) e1 e2))
> > > > list :key (lambda (elt) (list (funcall fun elt) elt)))))
> > > >
> > > > CL-USER 4 > (find-maximizing-item '(3 -4 2 5 -7 1 2) (lambda (x) (* x x)))
> > > > 49
> > > > -7
> >
> > Ocaml:
> >
>
>
> open List;;
>
>
>
> > let find_maximizing_item func list =
> > let rec loop best = function
> > [] -> best
> > | x::xs -> loop (max (func x, x) best) xs
> > in loop (func (hd list), hd list) (tl list) ;;
> >
> >
> > find_maximizing_item (fun x -> x*x) [3;-4;2;5;-7;1;2];;
> > ===>
> > (49, -7)

Shorter:

open List;;

let find_maximizing_item func list =
fold_left
(fun best x -> max (func x, x) best)
(func (hd list), hd list)
(tl list) ;;