This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

[Newbie] Help needed for basic problem

9 posts · 5456 views

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

(let ((min most-positive-fixnum))
(do (x list)
(when (> x min)
...)))

Re: [Newbie] Help needed for basic problem

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.
--Dan B.

Re: [Newbie] Help needed for basic problem

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

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.
(let ((list (list 1 2 3)))
  (do ((x list (cdr x)))
      ((not x))
    (print (car x))))

Re: [Newbie] Help needed for basic problem

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.
(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

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:
(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

Thank you very much for your help, I understand better.

S

Re: [Newbie] Help needed for basic problem

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.)
(defun list-lowest (list) (loop for el in list minimize el))
Could be more general imo:
(defun list-lowest (list &optional (lower #'<))
  (let ((min (car el))
    (dolist (el (cdrlist) (when (funcall lower el min) (setf min el)))
    el))