add function defined?

For the Compleat Fan

add function defined?

Postby ssqq » Mon Jul 06, 2015 8:44 am

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

ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: add function defined?

Postby TedWalther » Mon Jul 06, 2015 8:34 pm

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.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: add function defined?

Postby hartrock » Thu Aug 13, 2015 10:45 pm

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

Re: add function defined?

Postby TedWalther » Fri Aug 14, 2015 1:30 am

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.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: add function defined?

Postby hartrock » Fri Aug 14, 2015 8:41 am

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


Return to Anything else we might add?

Who is online

Users browsing this forum: No registered users and 1 guest

cron