add function defined?

For the Compleat Fan
Locked
ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

add function defined?

Post 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


TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: add function defined?

Post 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.
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
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

Re: add function defined?

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

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: add function defined?

Post by TedWalther »

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
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

Re: add function defined?

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

Locked