This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

passing formal arguments to scheme lambda functions?



[I also posted this on comp.lang.scheme, but I guess it is more
relevant here]

Hi,

I wanted to know whether one can somehow pass variables by reference
in scheme?  I looked around in places, but could'nt get much.  Here
are some of my futile attempts:

Say, I like to define a function swap(a,b) to swap values of a and b.
I understand that I have to quote a and b because in scheme, arguments
are evaluated before they are passed to functions. 

Now I know that such a behaviour can be achieved using define-macro,
but there must be a way without using macros.  I also want to achieve
this call by reference in a function written in C.

Thus, i want a function swap (written in C and/or scheme) behaving
like:

guile> (define a 10)
guile> (define b 20)
guile> a
10
guile> (swap 'a 'b)              ; this function i want
guile> a                         ; this should be new value of a
20
guile> b
10

I think the procedure symbol-set! comes closest to what I should use
in function swap, but I could make it work.

My first try:

(define (swap var1 var2)
  (if (or (not (symbol? var1))
	  (not (symbol? var2)))
      (error "atleast one argument is not a symbol!")
      (let ((tmp (eval var1)))
	(symbol-set! #f var1 (eval var2))
	(symbol-set! #f var2 tmp))))

guile> (swap 'a 'b)

swap.scm:7:9: In procedure symbol-set! in expression (symbol-set! #f var1 ...):
swap.scm:7:9: uninterned symbol? 
ABORT: (misc-error)
Type "(backtrace)" to get more information.

I tried putting (intern-symbol #f var1) before doing symbol-set!, but
I really don't grok what an intern symbol is, so I thought maybe you
guys could help :)

PS: Actually, at the end of the day I will like to set this variable
within C (using guile), so if you have any kind tips for that, please
tell them too!

Regards,
Lokesh Setia.
-- 
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/      .--.      |                                             _/ 
_/     |o_o |     |   Lokesh Setia <lsetia@hss.hns.com>         _/
_/     |:_/ |     |   Tel: +91-11-91-346666-x2167               _/
_/    //   \ \    |   #include <sys/disclaimer.h>               _/  
_/   (|     | )   |   Org: Hughes Software Systems              _/ 
_/  /'\_   _/`\   |                                             _/ 
_/  \___)=(___/   |                                             _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

-- 
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/      .--.      |                                             _/ 
_/     |o_o |     |   Lokesh Setia <lsetia@hss.hns.com>         _/
_/     |:_/ |     |   Tel: +91-11-91-346666-x2167               _/
_/    //   \ \    |   #include <sys/disclaimer.h>               _/  
_/   (|     | )   |   Org: Hughes Software Systems              _/ 
_/  /'\_   _/`\   |                                             _/ 
_/  \___)=(___/   |                                             _/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]