Re: scope of function definition
Raymond <[email protected]>
| Newsgroups | gmane.lisp.lush.devel |
|---|---|
| Message-ID | <[email protected]> |
Ralf, > I noticed that de, df, dm etc. do the binding of function > to symbol in lexical scope. > > ? (let ((f ())) (de f () 1)) > = f > ? f > = () > ? > > Unlike defvar, the descriptions for de etc. are not explicit > about the scope of the binding. I think they should be explicit. You can only do the binding globally if you do not do it within a let. When you use let you are in effect creating a new environment for everything contained inside it. Once you go out of scope anything defined inside is gone and you go back to the previous environment. Try this ? (de f () 1) = f ? f = ::DE:8892494 ;; new object comes into existence ? (f) = 1 ? (let ((f ())) (de f () 1) (f)) = 1 ? (let ((f ())) (de f () 1) f) = ::DE:88923c8 ;; another object comes into existence inside let ? f = ::DE:8892494 ? (f) 1 > Although I'm still a lisp newbie my impression is that it would > be more in line with other lisps if de/df/dm were doing the > binding globally (like, e.g., defun in CL). So if it doesn't > break too much, you might want to consider changing the > current behavior. What you are asking for is to propagate a local environments definitions/objects to another environment. When you do that you are relying on side effects to accomplish something--usually considered bad programming practice. One of the basis of Lisp is the ability to perform functional programming which means calling functions with data or functions and expecting data or functions as return values. The let construct returns the value of the last list/atom evaluated as it is supposed to. I know in Common lisp you can do what you are saying, but that does not mean it is the best way to do it and I cannot see why you would want to define a function inside a let anyway. You can define it at default environment level, separate it into another .lsh file and load it as necessary, or make it a class method instead. Also notice from my example that there are two f functions. One at the toplevel and the other inside the let. The way you are proposing will cause the definition of a new f in any sub environment to redefine the global f. That is not good and can lead to many side effects and bugs in a program. The current behavior seems good to me. Raymond ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click