Skip to main content
GameDev.net gamedev.net
🔒 Locked

clisp vs me

Started by BradDaBug Nov 25, 2004 at 10:23 PM 10 replies 1.6k views
Original Post
BradDaBug
BradDaBug
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.
I like the DARK layout!
skalco6
skalco6
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 ().
C-Junkie
C-Junkie
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 ().
skalco6
skalco6
;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)
Diodor
Diodor
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)))
BradDaBug
BradDaBug
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?
I like the DARK layout!
Woodsman
Woodsman
Can you show us the function call?
If a plant cannot live according to its nature, it dies; so a man.
Woodsman
Woodsman
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.
If a plant cannot live according to its nature, it dies; so a man.
Diodor
Diodor
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))
Mayrel
Mayrel
What Diodor said: setf

PS. Scheme is not Common Lisp. Giving someone a Scheme answer to a Common Lisp question is like giving someone a Fortran answer to a C question.
GnuVince
GnuVince
Quote:
Original post by BradDaBug
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.


You are not using let correctly:
(let ((mylist nil))  (dotimes (count 10)    (cons count mylist)))


However, this will return NIL since dotimes doesn't return anything.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.