This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Unbound variable problem

2 posts · 3900 views

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:
(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

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:
;; 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
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p