In article <····················@ucunix.san.uc.edu> ········@ucunix.san.uc.edu (Spandan Choudury) writes:
For codes of the form:
(defun f1 (aa)
(setq aa 5)
)
(defun test-f1
(setq a 10)
(f1 a)
a ;; X
)
What can be the simplest way of updating a (to 5) in step "X" when
"test-f1" is called (using a modified "f1") ?
How about
(defun f1 ()
5
)
(defun test-f1
(setq a 10)
(setq a (f1))
a ;; X
)
Other alternatives:
alternative A:
(defvar *a*)
(defun f1 *a*()
(setq *a* 5)
)
(defun test-f1
(setq *a* 10)
(f1 *a*)
*a* ;; X
)
This makes *a* visible throughout your program, and makes is dynamically
bound. E.g. in
(defun foo () (let ((*a* 3)) (bar)))
(defun bar () (print *a*))
(foo) prints 3.
The use of *'s in the name is a convention for human readability.
alternative B:
(let ((a))
(defun f1 (a) ;; note: a, not aa
(setq a 5)
)
(defun test-f1
(setq a 10)
(f1 a)
a ;; X
)
)
This makes a visible in the lexical scope of the let, and not elsewhere.
--
Lou Steinberg
uucp: {pretty much any major site}!rutgers!aramis.rutgers.edu!lou
internet: ···@cs.rutgers.edu
From: lawrence.g.mayka
Subject: Re: How update variable values by calling secondary functions ?
Date:
Message-ID: <LGM.92Jun4092733@cbnewsc.ATT.COM>
In article <················@atanasoff.rutgers.edu> ···@cs.rutgers.edu (lou) writes:
(let ((a))
(defun f1 (a) ;; note: a, not aa
(setq a 5)
)
(defun test-f1
(setq a 10)
(f1 a)
a ;; X
)
)
This needs a bit of cleanup before it will work properly:
(let ((a))
(defun f1 () ; Don't take an argument
(setq a 5) ; Access the closed-over variable instead
)
(defun test-f1 ()
(setq a 10)
(f1)
a
)
)
Lawrence G. Mayka
AT&T Bell Laboratories
···@iexist.att.com
Standard disclaimer.