Page 1 of 1
add function defined?
Posted: Mon Jul 06, 2015 8:44 am
by ssqq
I think newlisp should add function *defined?*, for all not defined variable is *nil*.
Code: Select all
> (defined? 'var) ; --> nil
> (set 'var nil)
> (defined? 'var) ; --> true
> (delete 'var)
> (defined? 'var) ; --> nil
Re: add function defined?
Posted: Mon Jul 06, 2015 8:34 pm
by TedWalther
Or perhaps a "strict" mode where unbound variables aren't auto-defined? With a new debug message, "symbol FOO is undefined"
The behavior where (++ foo) gives 1, is confusing if newLisp is trying to keep "nil" and "0" distinct from each other. Although (++ foo) => 1 is nice behavior and I like it. I'd like to know more about the need to separate nil from 0.
Re: add function defined?
Posted: Thu Aug 13, 2015 10:45 pm
by hartrock
ssqq wrote:
Code: Select all
> (defined? 'var) ; --> nil
> (set 'var nil)
> (defined? 'var) ; --> true
> (delete 'var)
> (defined? 'var) ; --> nil
This would not work, because using the symbol
'var defines it
before calling the function
defined?.
But there is:
Code: Select all
> ;; check for var 'var
> (sym "var" (context) nil)
nil
> ;; -> not there
> ;; now define it:
> var
nil
> ;; check
> (sym "var" (context) nil)
var
> ;; -> now it exists
>
The flag
nil in
(sym "var" (context) nil) is important, since it suppresses creating the
'var symbol.
Re: add function defined?
Posted: Fri Aug 14, 2015 1:30 am
by TedWalther
Wonder if that would work as (define-macro (defined? a) ...)
Re: add function defined?
Posted: Fri Aug 14, 2015 8:41 am
by hartrock
Just started with using emacros (expansion macros) here a simple solution (with limitations):
Code: Select all
> (macro (defined? V) (sym V (context) nil))
(lambda-macro (V) (expand '(sym V (context) nil)))
> (defined? "var")
nil
> var
nil
> (defined? "var")
var
> ; but:
> (defined? "V")
V
> ; -> because it is used as symbol by the macro
> (defined? 'v2)
v2
> ; -> because it is defined *before* calling defined?
A more general variant would allow to choose another as the current and/or all contexts to search for the sym (using at least one more emacro parameter variable then).
A naming convention for such general-purpose emacros could be, to only use
one-letter parameter names for such emacros:
- this avoids confusion with contexts, which may be named starting uppercase, too (but are usually multi-letter); and
- reduces sym pollution of MAIN context to a minimum.
Note: starting parameter names with uppercase is needed for emacros.