Insight, Help?

Discussion of Common Lisp

Insight, Help?

Postby cocoramzi » Thu Nov 03, 2011 6:54 pm


I wanted to create a function that verify if the numbers of a list fall between the range of 1 and 10. If they do, I wanted to add it to a new list that I created. However, if they fail to fall within the range I decided to convert them so they fit in rather than discarding them (by adding (1+ (random 10)).

The appearance of my function is as follow:

(defun fitrange (lst)
"(lst) check every number if it fits the range (1-10)"
(do ((fit lst (cdr fit))
(top nil (and (>= fit 1) (=< fit 10)) (cons fit top))
(or (< fit 1) (> fit 10))
(cons (+ fit (1+ (random 10))) top))))

When I run the function, it indicates error where fit is an unbound variable.
Any insight would be helpful, because any changes I tried turned into failure.
I'm fairly new to Lisp and the manual helped yet left me confused as well...
Thank you in advance. I appreciate it.
Posts: 1
Joined: Thu Nov 03, 2011 5:10 pm

Re: Insight, Help?

Postby Paul » Sat Nov 05, 2011 3:24 am

You have several problems there. There are misplaced parentheses, FIT is a list, not a number, and CONS only allocates a new cons cell (you use it as if it modifies TOP; you never use the value, so it just gets thrown away).

Code: Select all
(defun fitrange (list)
   (loop for fit in list if (<= 1 fit 10) collect fit else collect (+ fit (1+ (random 10)))))
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 8 guests