Hi Imran,

> The intention behind the code (see below) is pretty simple. (mk) will
> create a new namespace (well, a new symbol table), and (dset) should
> set a variable in a specified namespace.

For one thing, you can easily set a variable in another namespace with:

   (setq Dc~k1 123)

This works, btw, also with further indirections:

   (setq Dc~ns1~ns2~k1 777)

> However:
> + even though (dset) switches the active symbol table with (symbols Dict-ns)
> + and (symbols) returns <Dict-ns> as the active symbol table
> + but the variable |Key| is still only set in the pico symbol table

You must keep in mind that namespaces affect only the reader, not
expressions which already have been read. The namespace which is active
at the moment an expression is read determines which symbol with a given
name will be in the expression.

> 1) What am I doing wrong when I'm trying to (setq) the variable?

> (mk Dc)

creates a new namespace 'Dc', and immidiately sets the current namespace
back to 'pico'. That's all right. The expression

> (dset Dc k1 (pack (time) " elapsed seconds"))

is _read_ while 'pico' is the active namespace. A symbol 'k1' didn't
exist at the moment the above 'mk' was called, so an entry for that name
will be created in 'pico', and the expression '(setq Dc ...)' is
returned, and then evaluated. It is irrelevant what 'dset' does with its
arguments during that evaluation, the symbol 'k1' exists in 'pico'.

> 2) is there a way to create a new symbol-table/namespace without it
> becoming the active one?

Yes. The above 'mk' is fine for that.

> 3) is there a way to check if a symbol is interned, without actually
> interning it and setting it to NIL (if it didn't previously exist).
> Basically, some way to do (interned? 'sym) => T/NIL .

Yes, but I can't think of a peculiarly efficient way at the moment. You
could check with

   (member "foo" (all))

i.e. use a transient symbol to avoid the interning.

Technically, an efficient way would be possible (avoiding 'all', which
first collects all symbols into a list), by indexing directly into the
name tree. There is even such a function in the interpreter, i.e.
'isInternEXY_F' in src64/sym.l:37, but unfortunately there is no
top-level frontend for it.

Where would such a function be useful?

> 4) is there a way to concatenate two symbol-names together, without
> creating a transient symbol? (symbol+ 'foo 'bar) => 'foobar

No, but creating a transient symbol first does no harm. A transient
symbol is just a symbol which happens not to be referred to from the
current namespace. Each internal symbol is first created (by the reader)
as a transient symbol, and then immediately interned.

So (pack "foo" "bar") and (pack 'foo 'bar) both return "foobar", and
(intern (pack "foo" "bar")) is just fine.

- Alex
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to