the answer to my other question (thread "output either to file or stdout";
thanks to those who replied) made me wonder about the use of variables
inside flet defined outside the flet.
is there any reason to prefer this:
(defun my-func (arg1)
...
(flet ((local-func (arg)
(foo arg)))
...
(local-func arg1)))
over this:
(defun my-func (arg1)
...
(flet ((local-func ()
(foo arg1)))
...
(local-func)))
IOW is there any harm in creating the flet as a closure?
--
Joost Kremers ············@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
On Fri, 19 Sep 2008 12:32:14 +0000, Joost Kremers wrote:
> the answer to my other question (thread "output either to file or
> stdout"; thanks to those who replied) made me wonder about the use of
> variables inside flet defined outside the flet.
>
> is there any reason to prefer this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func (arg)
> (foo arg)))
> ...
> (local-func arg1)))
>
> over this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func ()
> (foo arg1)))
> ...
> (local-func)))
>
> IOW is there any harm in creating the flet as a closure?
None. It is quite an elegant solution.
Tamas
P� Fri, 19 Sep 2008 14:32:14 +0200, skrev Joost Kremers
<············@yahoo.com>:
> the answer to my other question (thread "output either to file or
> stdout";
> thanks to those who replied) made me wonder about the use of variables
> inside flet defined outside the flet.
>
> is there any reason to prefer this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func (arg)
> (foo arg)))
> ...
> (local-func arg1)))
>
> over this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func ()
> (foo arg1)))
> ...
> (local-func)))
>
> IOW is there any harm in creating the flet as a closure?
>
>
No har at all. In fact much of the reason to have lables and flet is to
allow this.
Consider:
(transpose (matrix)
(labels ((rec-transpose (matrix acc-row acc-result)
...))
(rec-transpose matrix nil nil))
or
(defun make-account ()
(let ((total 0))
(flet ((withdraw (amount) ...)
(add (amount) ...)
(saldo () ...))
(list :withdraw #'withdraw :add #'add :saldo #'saldo)))
(let ((account (make-account)))
(funcall (getf :add account) 100))
...)
--------------
John Thingstad
In article
<···························@j.kremers4.news.arnhem.chello.nl>,
Joost Kremers <············@yahoo.com> wrote:
> the answer to my other question (thread "output either to file or stdout";
> thanks to those who replied) made me wonder about the use of variables
> inside flet defined outside the flet.
>
> is there any reason to prefer this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func (arg)
> (foo arg)))
> ...
> (local-func arg1)))
>
> over this:
>
> (defun my-func (arg1)
> ...
> (flet ((local-func ()
> (foo arg1)))
> ...
> (local-func)))
>
> IOW is there any harm in creating the flet as a closure?
It's not a closure. It is just a local function that references
variables from the surrounding environment. It would
be a closure if you pass the function to another one
or return it from the scope where it is defined.
From a 'software engineering' point of view sometimes it is a bit
better to use the arglist:
* a function is kind of a 'module' and the parameter
makes clear what the input is
* it improves the reuse, since you can pass different
arguments. Compare that to the situation where
you would set the variable to another value and call
the function again.
* it helps with tracing local functions
* sometimes I move local functions out of
a function or into a function during some
'refactoring' or experimenting. If
the local function has a full arglist it gets easier.
Then one just cuts the local function...
(defun my-func (arg1)
...
(flet ((do-something (arg)
(foo arg)))
...
(do-something arg1)))
<==>
(defun do-something (arg)
(foo arg))
(defun my-func (arg1)
...
(do-something arg1))
In above case you can take the function and move
it in the source code, make only few changes and
a reindent -> the function is moved in source.
In the case of the local function I don't want
a global function (to keep the global space smaller
and to prevent others to call this function), but I
want to split the functionality in a few named
functions to improve the source code.
If the function would introduce some overhead,
sometimes it can be declared INLINE.
--
http://lispm.dyndns.org/
Rainer Joswig wrote:
> Joost Kremers <············@yahoo.com> wrote:
>> IOW is there any harm in creating the flet as a closure?
>
> It's not a closure. It is just a local function that references
> variables from the surrounding environment.
ah, ok.
> It would
> be a closure if you pass the function to another one
> or return it from the scope where it is defined.
i see.
> From a 'software engineering' point of view sometimes it is a bit
> better to use the arglist:
yes, i figured as much. that's not much of an issue for me right now, as
this is only a small program. i'll keep it in mind though. ;-)
thanks.
--
Joost Kremers ············@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)