Page 2 of 2

Re: Finding the minimum element in a list

Posted: Tue Jun 21, 2011 10:22 am
by Indecipherable
Not really in a loop, but I guess it can be put inside it :?

Code: Select all

(defun min-list (mlist)
   (eval `(min ,@mlist)))

Re: Finding the minimum element in a list

Posted: Wed Jun 22, 2011 3:18 am
by Paul
Indecipherable wrote:Not really in a loop, but I guess it can be put inside it :?

Code: Select all

(defun min-list (mlist)
   (eval `(min ,@mlist)))
No. Just, no!

Re: Finding the minimum element in a list

Posted: Wed Jun 22, 2011 3:52 pm
by gugamilare
Indecipherable wrote:Not really in a loop, but I guess it can be put inside it :?

Code: Select all

(defun min-list (mlist)
   (eval `(min ,@mlist)))
As a rule of thumb, don't use eval unless what you are doing really needs an evaluator (e.g. if you are generating functions on-the-fly or if you need an interpreter of some sort).

Re: Finding the minimum element in a list

Posted: Tue Jun 28, 2011 7:47 am
by Konfusius
Why not? It's perfectly legal and correct code. Using

Code: Select all

(defun min-list (mlist)
   (apply #'min mlist))
would be more efficient, though.

Re: Finding the minimum element in a list

Posted: Tue Jun 28, 2011 8:37 am
by gugamilare
It's slow, ugly and unnecessary. Also, Lispers who view your code will think you couldn't find a better way to deal with the problem because, in general, using eval when not needed is very common among newbies.

It would be more efficient to use reduce instead of apply (it has already been said in this thread) because, in general, it is faster to call functions with only two arguments then with many arguments. Not to mention that there is a limit on the number of arguments a function can take, so reduce would work more generally.

Re: Finding the minimum element in a list

Posted: Tue Jun 28, 2011 7:39 pm
by nuntius
EVAL is something like the UUOC of the lisp world...