add function defined?

Started by ssqq, July 06, 2015, 01:44:33 AM

Previous topic - Next topic

ssqq

I think newlisp  should add function *defined?*, for all not defined variable is *nil*.




> (defined? 'var) ; --> nil
> (set 'var nil)
> (defined? 'var) ; --> true
> (delete 'var)
> (defined? 'var) ; --> nil


TedWalther

#1
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.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

hartrock

#2
Quote from: "ssqq"


> (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:

> ;; 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.

TedWalther

#3
Wonder if that would work as (define-macro (defined? a) ...)
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

hartrock

#4
Just started with using emacros (expansion macros) here a simple solution (with limitations):
> (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.
  • [/list]

    Note: starting parameter names with uppercase is needed for emacros.