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")))
)
Code: Select all
> (l_nested (ref-parent (ref "14" l_nested)))
("13" "14" "15" ("16" "17" "18"))
Code: Select all
> (l_nested (ref-parent (ref "11" l_nested)))
ERR: missing argument
Code: Select all
> (ref-parent (ref "14" l_nested))
(7)
Code: Select all
> (ref-parent (ref "11" l_nested))
()
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")))
Because ref'ing a non-existing element gives:
Code: Select all
> (ref "not there" l_nested)
nil
If there is:
Code: Select all
> (ref-all "not there" l_nested)
()
Are there any problems with this proposal, which I don't see?
What do you think?
Feedback to this proposal is appreciated (as usual).