Jump to content
  • Advertisement
Sign in to follow this  
BradDaBug

clisp vs me

This topic is 4957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's what I'm trying to do. I want to fill a list with values, maybe numbers from 0 to 10. So here's what I'm doing:
(let mylist 0)
(dotimes (count 10)
  (cons count mylist))
But it says that mylist has no value. I figure this is a really easy thing to fix but I can't seem to get it working.

Share this post


Link to post
Share on other sites
Advertisement
I'm currently learning scheme (with DrScheme) at university. I can't tell how different it is from common lisp, but as far as I know, an empty list is not 0, but ().

Share this post


Link to post
Share on other sites
cons is not a mutator. It doesn't change the value of anything.

Take a look at set! (i think that's clisp... I'm too used to scheme)

All mutators have a bang! at the end.

(also, unless I'm mistaken, in order to make a real list, you should be cons'ing all the elements onto a null ().

Share this post


Link to post
Share on other sites
;this works quite well in scheme


(define (fill-list start end)
(define (iter mylist n)
(if (< n start)
mylist
(iter (cons n mylist) (- n 1))))
(iter (list end) (- end 1)))

(fill-list 5 15)



(5 6 7 8 9 10 11 12 13 14 15)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Check the scoping rules for let-expressions. mylist isn't bound to anything, and hence not defined, in your loop.

Share this post


Link to post
Share on other sites
Pick one:

(setq mylist '(0 1 2 3 4 5 6 7 8 9))

(setq mylist (loop for i below 10 collect i))

(setq mylist
(let ((lst))
(dotimes (x 10)
(push x lst))
(reverse lst)))

Share this post


Link to post
Share on other sites
Thanks for the help!

Now I've got another problem. I'm trying to send 'd (the literal "d") to a function that inserts it into a list. Here's what the function looks like:
(defun setnth(mylist n x)
(setq mylist
(loop for k below (length mylist) collecting
(if (= k n)
(eval x)
(nth k mylist)
)
)
)
)

But when I call that function I get an error message that says "variable D has no value." What am I doing wrong now?

Share this post


Link to post
Share on other sites
Well, going on an assumption.

I'm imagining that you're using this:

(setq x '(1 2 3))
(1 2 3)
(setnth x 2 'd)
;;; An error occurred in function #< COMPILED-FUNCTION: #xE239B8 >:
;;; Error: The variable D is unbound


The problem appears to be (eval x) where you're saying (eval d)

Remember, with functions, the arguments are evaluated first, so the function gets d, not 'd.

I haven't actually looked at the code for logic errors, mind you.

Share this post


Link to post
Share on other sites
Where should I begin.

Your indenting style is very ugly. Don't close brackets like you are writing C.

The error comes from the (eval x) command. Normally you don't need to use eval while you're learning Lisp. eval will take some Lisp code in the form of a Lisp list and execute it. If you pass your function the symbol 'd through the parameter x, (eval x) tries to return the value of the variable d. It's just as if you wrote d instead of (eval x). Since there is no binding for a variable d, the program fails.

So you should have simply written x instead of (eval x).

The other problem comes from using (setq mylist. mylist is a local variable. Even if you change its binding with setq, that change is not visible outside the function.

Here's your function that returns the new list rather than modifying the mylist parameter:


(defun setnth(mylist n x)
(loop for k below (length mylist) collecting
(if (= k n)
x
(nth k mylist))))

Or you could do it like this:

(defun setnth(mylist n x)
(loop for k from 0
for y in mylist
collect (if (= k n) x y)))




If you want to modify a list that is passed as a parameter to a function you should look into the rplaca or rplacd functions. Of course you could simply:


(defun setnth (mylist n x)
(setf (nth n mylist) x))


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!