[Newbie] Help needed for basic problem

Discussion of Common Lisp
Post Reply
Sarah44
Posts: 3
Joined: Sat Jan 31, 2009 6:03 pm

[Newbie] Help needed for basic problem

Post by Sarah44 » Sat Jan 31, 2009 6:09 pm

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

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

Re: [Newbie] Help needed for basic problem

Post by nuntius » Sat Jan 31, 2009 8:24 pm

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

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

Re: [Newbie] Help needed for basic problem

Post by danb » Sun Feb 01, 2009 6:20 am

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.

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

Re: [Newbie] Help needed for basic problem

Post by Sarah44 » Sun Feb 01, 2009 3:37 pm

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

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

Re: [Newbie] Help needed for basic problem

Post by nuntius » Sun Feb 01, 2009 7:06 pm

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))))

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

Re: [Newbie] Help needed for basic problem

Post by Paul Donnelly » Sun Feb 01, 2009 9:17 pm

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)))

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

Re: [Newbie] Help needed for basic problem

Post by danb » Mon Feb 02, 2009 7:44 am

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>))

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

Re: [Newbie] Help needed for basic problem

Post by Sarah44 » Mon Feb 02, 2009 6:02 pm

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

S

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

Re: [Newbie] Help needed for basic problem

Post by Jasper » Sat Feb 14, 2009 8:14 am

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))

Post Reply