(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.
clisp vs me
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:
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 ().
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 ().
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 ().
;this works quite well in scheme
(5 6 7 8 9 10 11 12 13 14 15)
(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)
Check the scoping rules for let-expressions. mylist isn't bound to anything, and hence not defined, in your loop.
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)))
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:
But when I call that function I get an error message that says "variable D has no value." What am I doing wrong now?
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?
Well, going on an assumption.
I'm imagining that you're using this:
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.
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.
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:
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:
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))
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement