[Newbie] Help needed for basic problem
[Newbie] Help needed for basic problem
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
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)
...)))
(do (x list)
(when (> x min)
...)))
Re: [Newbie] Help needed for basic problem
Use DOLIST to iterate over a simple list.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.
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.Could somedy hint me on how to get started with that function?
--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
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.
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))))
-
- Posts: 148
- Joined: Wed Jul 30, 2008 11:26 pm
Re: [Newbie] Help needed for basic problem
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.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?
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
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:Sarah44 wrote:the textbook says that we can solve the problem using only do
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
Thank you very much for your help, I understand better.
S
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.)
Could be more general imo:
Code: Select all
(defun list-lowest (list) (loop for el in list minimize el))
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))