## [Newbie] Help needed for basic problem

Discussion of Common Lisp

### [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
Sarah44

Posts: 3
Joined: Sat Jan 31, 2009 6:03 pm

### Re: [Newbie] Help needed for basic problem

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

nuntius

Posts: 532
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

### 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.
danb

Posts: 35
Joined: Sat Jun 28, 2008 1:05 pm
Location: Urbana, Illinois, US

### 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
Sarah44

Posts: 3
Joined: Sat Jan 31, 2009 6:03 pm

### 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.
Code: Select all
`(let ((list (list 1 2 3)))  (do ((x list (cdr x)))      ((not x))    (print (car x))))`

nuntius

Posts: 532
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

### 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.
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)))`
Paul Donnelly

Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### 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:
Code: Select all
`(do ((cell list (cdr cell)))    ((not cell))  (let ((x (car cell)))    <insert the body of your code here>))`
danb

Posts: 35
Joined: Sat Jun 28, 2008 1:05 pm
Location: Urbana, Illinois, US

### Re: [Newbie] Help needed for basic problem

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

S
Sarah44

Posts: 3
Joined: Sat Jan 31, 2009 6:03 pm

### 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.)
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))`
Jasper

Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands

Return to Common Lisp

### Who is online

Users browsing this forum: No registered users and 5 guests