From: Leigh Rutland
Subject: Simple help required.  Bad argument or unbound variable
Date: 
Message-ID: <9kbkgscsahm4guv35po24pficr03b9beom@4ax.com>
Can someone please help me.  i have some coursework due in soon and am
having terrible trouble with a function i have written.

When i try to run the function (connect) as follows:
>(connect 'london reading)
i get the error: Unbound variable - READING

if i run the function:
>(connect 'london 'reading)
i get the error: bad argument type - N2

The code is:


;The following code assigns the relevant neighbours
 ;to the property 'nieghbours' of each city atom
 
 ;assign values for atom London
 (putprop 'London '(Basingstoke Reading Oxford Birmingham)
'neighbours)

 ;assign values for atom Basingstoke
 (putprop 'Basingstoke '(Reading London Oxford) 'neighbours)

 ;assign values for atom Oxford
 (putprop 'Oxford '(Basingstoke Reading London Birmingham)
'neighbours)

 ;assign values for atom Birmingham 
 (putprop 'Birmingham '(London Oxford) 'neighbours)

 ;assign values for atom Reading
 (putprop 'Reading '(Oxford Basingstoke London) 'neighbours)

 
 
 ;Question 2
 
 ;The following code defines a function 'Connect'
 ;that takes two cities as parameters.
 ;The fucntion then adds a connection between the cities
 ;if one doesn't already exist
 ;if the connection exists then nothing is changed
 
 ;function definition
 (defun connect (city_1 city_2)
 	
	;assign city_1 and city_2 neighbours to variables n1 n2
 	(setq n1 (get city_1 'neighbours))
 	(setq n2 (get city_2 'neighbours)) 
 
 		;check if city_1 exists in n2's neighbours list
 		(cond
 			((member city_1 'n2))
 			;if city_1 doesn't exist add to n2's
neighbours list
 			((setq n2 (cons city_1 n2))
 				;assign revised list to city_2's
neighbours property
 				(putprop city_2 n2 'neighbours)))
 
 		;check if city_2 exists in n1's neighbours list
 		(cond
 			((member city_2 'n1))
 			;if city_2 doesn't exist add to neighbours
list
 			((setq n1 (cons city_2 n2))
 				;assign revised list to city_1's
neighbours property
 				(putprop city_1 n1 'neighbours)))
  ;end function
  ) 



Any help would be much appreciated.

Cheers, 

Leigh.

mailto: ·····@khamsynn.screaming.net
From: Rahul Jain
Subject: Re: Simple help required.  Bad argument or unbound variable
Date: 
Message-ID: <8eddl3$j35$1@joe.rice.edu>
In article <··································@4ax.com> posted on Friday,
April 28, 2000  7:41 PM, Leigh Rutland <·····@khamsynn.screaming.net> wrote:
> Can someone please help me.  i have some coursework due in soon and am
> having terrible trouble with a function i have written.
> 
> When i try to run the function (connect) as follows:
>>(connect 'london reading)
> i get the error: Unbound variable - READING
> 
> if i run the function:
>>(connect 'london 'reading)
> i get the error: bad argument type - N2
> 
[snip]
>  
>  
>  ;Question 2
>  
>  ;The following code defines a function 'Connect'
>  ;that takes two cities as parameters.
>  ;The fucntion then adds a connection between the cities
>  ;if one doesn't already exist
>  ;if the connection exists then nothing is changed
>  
>  ;function definition
>  (defun connect (city_1 city_2)
>  	
> 	;assign city_1 and city_2 neighbours to variables n1 n2
>  	(setq n1 (get city_1 'neighbours))
>  	(setq n2 (get city_2 'neighbours)) 

You haven't defined what N1 and N2 are yet. The usual way to define a local
variable is with a LET, so I would change the above code snippet to this:

(defun connect (city_1 city_2)
  (let ((n1 (get city_1 'neighbours))
        (n2 (get city_2 'neighbours)))

Here, the local variables N1 and N2 are defined and assigned appropriate
values. Personally, I'd get rid of the underscores, too, but that's just a
stylistic preference

>  		;check if city_1 exists in n2's neighbours list
>  		(cond
>  			((member city_1 'n2))
>  			;if city_1 doesn't exist add to n2's
> neighbours list

you shoudn't quote N2 here, you want to pass the list, not the symbol, to
MEMBER. (member city_1 n2)

>  			((setq n2 (cons city_1 n2))
>  				;assign revised list to city_2's
> neighbours property
>  				(putprop city_2 n2 'neighbours)))

You're using COND incorrectly here. The structure of COND is as follows:
(cond
  (condition code)
  (condition code)
  ... )
so what you'd really want is something more like:

(cond
  ((member city_1 n2) (setq n2 (cons city_1 n2)))

now I'm not sure if you really want a COND... would WHEN be a better control
structure for this case?

>  		;check if city_2 exists in n1's neighbours list
>  		(cond
>  			((member city_2 'n1))
>  			;if city_2 doesn't exist add to neighbours
> list
>  			((setq n1 (cons city_2 n2))
>  				;assign revised list to city_1's
> neighbours property
>  				(putprop city_1 n1 'neighbours)))
>   ;end function
>   ) 
> 

This code looks similar to what's above. The same suggestions apply.

Start with these suggestions and see how far they get you. You might want to
use the online Hyperspec as a language reference:
http://www.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/FrontMatter/index.html

Successful Lisp is good if you're looking for a tutorial:
http://psg.com/~dlamkins/left/sl/html/contents.html

Marty Hall's Lisp page is an excellent page to keep bookmarked as well:
http://www.apl.jhu.edu/~hall/lisp.html

I hope this helps.

-- 
-> -\-=-=-=-=-=-=-=-=-=-/^\-=-=-=<*><*>=-=-=-/^\-=-=-=-=-=-=-=-=-=-/- <-
-> -/-=-=-=-=-=-=-=-=-=/ {  Rahul -<>- Jain   } \=-=-=-=-=-=-=-=-=-\- <-
-> -\- "I never could get the hang of Thursdays." - HHGTTG by DNA -/- <-
-> -/- http://photino.sid.rice.edu/ -=- ·················@usa.net -\- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
   Version 11.423.999.210020101.23.50110101.042
   (c)1996-2000, All rights reserved. Disclaimer available upon request.