• .Re: using loop macro to find argmax

    From Robert L.@21:1/5 to Barry Margolin on Wed Feb 23 04:28:00 2022
    On Mon, 12 May 2003, Barry Margolin wrote:

    Using the loop macro you can find the maximum of some numerical function
    f, applied
    to each element of a list by
    (loop for x in list maxmimize (funcall f x))

    But I want to return the x such that (funcall f x) is maximum
    and I would like to write something like this:

    (loop for x in list argmaximize (funcall f x))

    Is there some loop keyword that would do this?
    If there isn't such a keyword what's the most succinct way to
    do this with the loop macro?

    There isn't a keyword for it.

    (loop with max-val = (car list)
    for x in (cdr list)
    when (> (funcall f x) max-val)
    do (setq max-val x))

    That doesn't function correctly.

    Gauche Scheme:

    (reduce
    (lambda (a b) (if (> (square a) (square b)) a b))
    'list-was-empty
    '(2 -8 4 6))

    ===>
    -8

    That calls the function more times than necessary.

    (define lst '(2 -8 4 6))

    (fold
    (lambda (a b)
    (define res (square a))
    (if (or (null? b) (> res (car b))) (list res a) b))
    '()
    lst)

    ===>
    (64 -8)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)