Page 1 of 1

Unbound variable problem

PostPosted: Tue Nov 26, 2013 12:49 am
by jameschristian
Hi all,

While this is not exactly a homework question, it's so basic that I thought it'd be better to post it here instead of on the common clip forum. Anyway, my problem is that I am running a really simple script that looks like this:
Code: Select all

(defun test (X List)
(member 'X 'List))


but when I go into gcl or clisp I get an error when I try (test X (A B C X Y)), when instead I'm expecting to get (X Y). The error says that the variable X is unbound. Anyone have any idea why? I've looked around but can't find anything on what I am sure is a very simple syntax issue.

Thanks!

Re: Unbound variable problem

PostPosted: Thu Nov 28, 2013 3:58 pm
by sylwester
So quoting works like everything you put ' in front of becomes the literal thing you're seeing.. What it means is that it WONT be seen as a variable and lisp won't try to turn it into it's value, which it got from either being a parameter or a variable.

Here is how to translate a line of code into a line of code calling a function that does the same:
Code: Select all
;; When you're looking for symbol A in list (W E A D E) you do:
(member 'A '(W E A D E)) ; ==> (A D E)

;; make it a function
(defun myfind (needle haystack)
  (member needle haystack)) ; neither needle nor haystack should be quoted!

;; but when you use them in the same manner as member you need to quote the literal values
(myfind 'A '(W E A D E)) ; == >(A D E)

;; Now you might have one or both as variables. Lets make em..
(setq W 'A)                  ; A is a literal since it's quoted with '
(setq list '(W E A D E)) ; (W E A D E) is a literal since it's quoted
;; no lets try using them
(myfind 'W list)  ; ==> (W E A D E) since the 'W doesnæt mean the variable but literal W
(myfind W list)  ; ==> (A D E) since W means the variable W which happens to be bound to literal A


So, as you can see. When we use variables we defenitely don't quote them. It's ONLY when we want A to mean the symbol When you use variables you don't quote.

Hope this helps :)

BR
Syl