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.
Could somedy hint me on how to get started with that function?
(let ((list (list 1 2 3)))
(do ((x list (cdr x)))
(print (car x))))
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?
(defun list-length (list &optional (so-far 0))
(list-length (cdr list) (1+ so-far))
(defun list-length-2 (list)
(do ((list list (cdr list))
(so-far 0 (1+ so-far)))
((null list) so-far)))
Sarah44 wrote:the textbook says that we can solve the problem using only do
(do ((cell list (cdr cell)))
(let ((x (car cell)))
<insert the body of your code here>))
(defun list-lowest (list) (loop for el in list minimize el))
(defun list-lowest (list &optional (lower #'<))
(let ((min (car el))
(dolist (el (cdrlist) (when (funcall lower el min) (setf min el)))
Users browsing this forum: No registered users and 5 guests