Unbound variable problem

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

Unbound variable problem

Postby jameschristian » Tue Nov 26, 2013 12:49 am

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!
jameschristian
 
Posts: 2
Joined: Tue Nov 26, 2013 12:42 am

Re: Unbound variable problem

Postby sylwester » Thu Nov 28, 2013 3:58 pm

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
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 84
Joined: Mon Jul 11, 2011 2:53 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 0 guests

cron