What is the convention for returning a side-effect in Scheme?
Question
I'm going through the Structure and Interpretation of Computer Programs MIT video lecture series, and I had a quick question about returning side-effects from functions.
In video 3A, the professor writes a quick equation for for-each
similar to this:
(define (for-each p l)
(if (null? l)
"done"
(p (car l)
(for-each p (cdr l)))))
Is there a specific convention for returning a side-effect from a function in Scheme, or was the "done"
an arbitrary choice?
Solution
That's not really a side effect, but rather that every Scheme function must return something. The string "done"
is something. Normally, when you call for-each
you might do something like:
(for-each (lambda (x) (display x)) lst)
and ignore the return value from for-each
. The author could have just as easily chosen any other value to return from the base case.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow