• Re: How do i get multiple Min() values?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.python,comp.lang.lisp on Thu Aug 15 03:33:46 2024
    From Newsgroup: comp.lang.python

    B. Pym wrote:

    HenHanna wrote:


    How do i get multiple Min() values?

    e.g. for Y = (x-2)*(x-3) for x in range(-10,10)
    the min Y is hit twice


    print( min( ((x-2)*(x-3), (x, (x-2, x-3)))
    for x in range(-10,10) ) )



    is this easy in Scheme(Gauche) ?

    Gauche Scheme

    (use gauche.collection) ;; fold2

    (define (min-by fn lst)
    (if (null? lst)
    (values '() #f)
    (fold2
    (lambda (x best worth)
    (let ((score (fn x)))
    (cond ((< score worth) (values (list x) score))
    ((= score worth) (values (cons x best) worth))
    (#t (values best worth)))))
    (take lst 1) (fn (car lst))
    (cdr lst))))

    (min-by (lambda(x) (* (- x 2) (- x 3))) (lrange -10 11))

    ===>
    (3 2)
    0

    newLISP

    (define (min-by fun lst)
    (let (accum '() best 0 score 0)
    (dolist (x lst)
    (setf score (fun x))
    (cond ((or (empty? accum) (< score best))
    (setf best score)
    (setf accum (list x)))
    ((= score best) (push x accum))))
    (list accum best)))

    (min-by (fn(n) (- (abs (* 10 n)))) '(0 9 2 2 -9 3))
    ===>
    ((-9 9) -90)


    --- Synchronet 3.20a-Linux NewsLink 1.114