Page 1 of 1

[proposal] unification: using '() as list identity deref arg

Posted: Wed Nov 27, 2013 6:14 pm
by hartrock
[update] -> 10.5.6.

During writing some code to ease navigation in lists, I've stumbled about something. Let's speak code for itself.

Assumed there is a definition for ref-parent and some example list:

;; nil for non-existing parent

Code: Select all

(define (ref-parent r)
  (if (null? r) nil (0 -1 r)))
;;
(setq l_nested '("1" "2" "3"
                  ("4" "5" "6"
                   ("7" "8" "9"))
                 "10" "11" "12"
                  ("13" "14" "15"
                   ("16" "17" "18")))
      )
Now it's possible to eval:

Code: Select all

> (l_nested (ref-parent (ref "14" l_nested)))
("13" "14" "15" ("16" "17" "18"))
; but it is not possible to eval:

Code: Select all

> (l_nested (ref-parent (ref "11" l_nested)))

ERR: missing argument
Reason is, that:

Code: Select all

> (ref-parent (ref "14" l_nested))
(7)
; but:

Code: Select all

> (ref-parent (ref "11" l_nested))
()
: here '() is not being accepted as deref argument.

What about introducing '() as allowed deref argument, which dereferences list itself to which it is applied to?
Then the following would work:

Code: Select all

> (l_nested (ref-parent (ref "11" l_nested)))
("1" "2" "3" ("4" "5" "6" ("7" "8" "9")) "10" "11" "12" ("13" "14" "15" ("16" "17" "18")))
This seems to be a neat unification regarding dereferencing lists by refs.

Because ref'ing a non-existing element gives:

Code: Select all

> (ref "not there" l_nested)
nil
; '() seems to be free for this purpose.
If there is:

Code: Select all

> (ref-all "not there" l_nested)
()
, we get a '(). But this is no problem, because for dereferencing refs gotten this way, we have to iterate through this list, which would do nothing here.

Are there any problems with this proposal, which I don't see?

What do you think?

Feedback to this proposal is appreciated (as usual).

Re: [proposal] unification: using '() as list identity deref

Posted: Wed Nov 27, 2013 8:43 pm
by Lutz
I like the idea: http://www.newlisp.org/downloads/develo ... 10.5.6.txt

... working as expected on your example.

Re: [proposal] unification: using '() as list identity deref

Posted: Fri Nov 29, 2013 12:42 am
by hartrock
Thanks: it works like a charm :-)
Could be a speed record from making a proposal to getting it done... :-)

Re: [proposal] unification: using '() as list identity deref

Posted: Fri Nov 29, 2013 4:02 pm
by xytroxon
No...

He usually adds the functionality before you have fully described (or understood), what functionality it is that you want added...

Then to add insult to injury, his solution works better and is more elegant... (Although it may take you a day or two to fully realize it ;o)

-- xytroxon

Re: [proposal] unification: using '() as list identity deref

Posted: Mon Dec 02, 2013 7:50 am
by xytroxon
Case in point, from this weekends inprogress newlisp-10.5.6.tgz

I was just thinking about how integer numbers could be used as hashkeys, and I find this addition in CHANGES-10.5.6.txt
Integers are accepted as hash keys. This allows creating sparse vectors:
(new Tree 'V)
(V 123 "hello")
(V 123) => "hello"
( I thought Lutz's PhD was in psychology, not parapsychology!!! ;o)

-- xytroxon

Re: [proposal] unification: using '() as list identity deref

Posted: Mon Dec 02, 2013 7:08 pm
by TedWalther
xytroxon wrote:Case in point, from this weekends inprogress newlisp-10.5.6.tgz

I was just thinking about how integer numbers could be used as hashkeys, and I find this addition in CHANGES-10.5.6.txt
Integers are accepted as hash keys. This allows creating sparse vectors:
(new Tree 'V)
(V 123 "hello")
(V 123) => "hello"
( I thought Lutz's PhD was in psychology, not parapsychology!!! ;o)

-- xytroxon
Sweet!