Page 1 of 1

[Newbie] Help needed for basic problem

Posted: Sat Jan 31, 2009 6:09 pm
by Sarah44
Hello,

I have been trying to write a function that will allow me to return the smallest number of a list. It is an exercice to practice "do" iteration.

Could somedy hint me on how to get started with that function? I am really confused with do iterations...

Thank you very much,
S

Re: [Newbie] Help needed for basic problem

Posted: Sat Jan 31, 2009 8:24 pm
by nuntius
(let ((min most-positive-fixnum))
(do (x list)
(when (> x min)
...)))

Re: [Newbie] Help needed for basic problem

Posted: Sun Feb 01, 2009 6:20 am
by danb
Sarah44 wrote:I have been trying to write a function that will allow me to return the smallest number of a list. It is an exercice to practice "do" iteration.
Use DOLIST to iterate over a simple list.
Could somedy hint me on how to get started with that function?
Basically, you start with the first element of the list, compare it with the next element, compare the smaller of the two values with the third element, and so on. Store the first element in a local variable and iterate down the CDR of the list, always storing the lowest value so far in that variable. If the list is empty, return NIL.

Re: [Newbie] Help needed for basic problem

Posted: Sun Feb 01, 2009 3:37 pm
by Sarah44
Thank you for your replies.

Danb, I think I am starting to understand. However, the only thing is that the textbook says that we can solve the problem using only do. Is it really different from dolist? If yes, could you explain in what it is different?

Thank you,
S

Re: [Newbie] Help needed for basic problem

Posted: Sun Feb 01, 2009 7:06 pm
by nuntius
Sorry; my first reply was botched; I wrote DO but used the syntax for DOLIST.

Here's a simple, non-broken example of DO iterating over a list.

Code: Select all

(let ((list (list 1 2 3)))
  (do ((x list (cdr x)))
      ((not x))
    (print (car x))))

Re: [Newbie] Help needed for basic problem

Posted: Sun Feb 01, 2009 9:17 pm
by Paul Donnelly
Sarah44 wrote:However, the only thing is that the textbook says that we can solve the problem using only do. Is it really different from dolist? If yes, could you explain in what it is different?
DO is like C(++ or 99 specifically, I think)'s for loop, except you can have multiple variables being stepped at once, and it returns a value (either nil or a value you specify), like every Lisp form. The basic idea is that you specify some variables, each optionally having an initial value and a form that produces the next value, then you specify a termination condition, then you write the loop body.

EDIT: It's also sort of a recursion emulation. Note the similarity between the arguments to the recursive LIST-LENGTH call and the next value forms in the DO loop.

Code: Select all

(defun list-length (list &optional (so-far 0))
  (if list
      (list-length (cdr list) (1+ so-far))
      so-far))

(defun list-length-2 (list)
  (do ((list list (cdr list))
       (so-far 0 (1+ so-far)))
      ((null list) so-far)))

Re: [Newbie] Help needed for basic problem

Posted: Mon Feb 02, 2009 7:44 am
by danb
Sarah44 wrote:the textbook says that we can solve the problem using only do
That's right. DO is an all-purpose iteration macro, and DOLIST is specialized for lists. (dolist (x list) <insert the body of your code here>) is almost equivalent to this:

Code: Select all

(do ((cell list (cdr cell)))
    ((not cell))
  (let ((x (car cell)))
    <insert the body of your code here>))

Re: [Newbie] Help needed for basic problem

Posted: Mon Feb 02, 2009 6:02 pm
by Sarah44
Thank you very much for your help, I understand better.

S

Re: [Newbie] Help needed for basic problem

Posted: Sat Feb 14, 2009 8:14 am
by Jasper
Ehm, no-one though of this: (The macro of the Iterate library seems to be a better macro, but i am too lazy to learn it.)

Code: Select all

(defun list-lowest (list) (loop for el in list minimize el))
Could be more general imo:

Code: Select all

(defun list-lowest (list &optional (lower #'<))
  (let ((min (car el))
    (dolist (el (cdrlist) (when (funcall lower el min) (setf min el)))
    el))