dillo gimp wrote:
hi
Is there a way to return "nothing"? so that : (list nothing 1 2 3 nothing 4 5 6) ; => (1 2 3 4 5 6)
I asked the same question on "comp.lang.scheme", but someone says it can't be done. Is this true?
;; (list (if (> 1 2) 3) (if (> 1 2) 3 nil)) ; -> (#!void ())
both "void" and '() are not exactly the same as "absolutely nothing". It would helps a lot if I can code something like that.
Scheme is not Perl with it's autosplicing "list contexts". But it has a syntax for this, quasiquote and unquote-splicing:
`(,@(if (> 1 2) (list 3) '()) ...)
e.g. always return lists from the producer then splice it explicitely.
It depends on the context whether other solutions are appropriate.
If you're producing lists recursively and want a flat result, for example, the clean approach is to feed the tail of the result to the inner invocations as their starting/tail value (that's more efficient than creating the sublists with '() as tail value and then appending the lists afterwards (as the above unquote-splicing does), and is still quite logical). Use the generic fold / fold-right functions for this, or write functions which take an #!optional (tail '()).
When writing macros I also sometimes flatten the list afterwards, I've even written a combined flat-append-strings function for this (for an example, see http://scheme.mine.nu/gambit/scratch/flat/cj-c-util.scm). This works well when you have the same object type only in the list, like strings.
SXML ignores #f and '() when serializing (at least my serializer does so), so you can just output those values without worrying.
Christian.