On 4/29/07, Christian Jaeger christian@pflanze.mine.nu wrote:
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.
Is it a good idea hacking the interpreter to automatically ignore all <#void>? I don't know if <#void> servers any purpose... "length" actually counts <#void> as well. There are legitmate reason for returning nothing, either because the caller has been written yet, or that it would be ugly to push the logic to the caller.
I'm doing polynomial addition and it's possible the term could be eliminated: If I push the logic to the caller, the code looks ugly. Besides, the code hasn't been written yet. It would be better for the language to support it, instead of people each hacking a different interpreter...
(define (TaddT x y) (if (equal? (cadr x) (cadr y)) (if (not (= (+ (car x) (car y)) 0)) (list (list (+ (car x) (car y)) (cadr x))) '() ) '() ) ) `(aaa ,@(TaddT '(2 (1 2 3)) '(3 (4 5 6))) aaa) (length `(aaa ,@(TaddT '(2 (1 2 3)) '(3 (4 5 6))) aaa)) `(aaa ,@(TaddT '(2 (1 2 3)) '(3 (1 2 3))) aaa) (length `(aaa ,@(TaddT '(2 (1 2 3)) '(3 (1 2 3))) aaa)) `(aaa ,@(TaddT '(-3 (1 2 3)) '(3 (1 2 3))) aaa) (length `(aaa ,@(TaddT '(-3 (1 2 3)) '(3 (1 2 3))) aaa))
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.