argmax
- From: "Alex Mizrahi" <udodenko@xxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Feb 2008 20:22:33 +0200
coding armax functionality each time from scratch have bored me to hell, so
i've decided to make a macro for this. i.e. simple case:
CL-USER> (with-find-optimal (check >)
(loop for i from 1 to 100
do (check i)))
100
100
with key being a function:
CL-USER> (with-find-optimal (check > -)
(loop for i from 1 to 100
do (check (cons i i))))
1
-1
with key being custom code:
CL-USER> (with-find-optimal (check >
(lambda (cons) (* (car cons) (cdr cons))))
(loop for i from 1 to 100
do (check (cons i i))))
(100 . 100)
10000
i suspect i'm not the only one doing such macro, but i'm not able to find
any of them, and don't even know how it could be called..
something tells me that fancy LOOP substitutions like ITERATE have this
built-in, but it would be overkill to use ITERATE just for this thing.
also, with my macro CHECK is lexical function, so it can be used in fairly
complex cases -- passed to other functions etc.
here's my code. i don't really like it (nor i like interface), but it seems
to work..
(defmacro with-find-optimal ((check compare &optional key) &body body)
(unless key (setf key 'identity))
(with-gensyms (option optimal optimal-value initialized option-value)
`(let (,optimal ,optimal-value ,initialized)
(flet ((,check (,option)
(let ((,option-value (,key ,option)))
(if ,initialized
(when (,compare ,option-value ,optimal-value)
(setf ,optimal ,option
,optimal-value ,option-value))
(setf ,optimal ,option
,optimal-value ,option-value
,initialized t)))))
,@body
(values ,optimal ,optimal-value)))))
.
- Follow-Ups:
- Re: argmax
- From: Maciej Katafiasz
- Re: argmax
- Prev by Date: Re: DIRECTORY behavior with /= implementations
- Next by Date: Re: System compilation failure, CLPPCRE, ASDF, SBCL involved
- Previous by thread: any reccomended chess programs in common lisp?
- Next by thread: Re: argmax
- Index(es):
Relevant Pages
|