As noted in the comment, your code currently looks like it has a (admittedly fairly minor) problem. In sum-multiples
, you try to pass actual
and limit
as parameters to sum-multiples-rec
, but those names aren't bound to anything at that point. You need/want something more like:
(define (sum-multiples lower-limit upper-limit)
(sum-multiples-rec lower-limit upper-limit 0))
As far as style goes, the main thing I'd change would be to write your ancillary functions inside of sum-multiples
. Right now, sum-multiples-rec
, count-check
and check
are all "publicly" visible, but they're unlikely to be of any use except to count-multiples
. As such, they should really be "hidden" inside it.
As such, your code could end up something like this:
(define (sum-multiples lower-limit upper-limit)
(define (sum-multiples-rec actual limit acc)
(define (count-check x)
(define (check x) (or (= (modulo x 3) 0)
(= (modulo x 5) 0)))
(if (check x)
x
0))
(if (< actual limit)
(sum-multiples-rec (+ actual 1) limit (+ (count-check actual) acc))
acc))
(sum-multiples-rec lower-limit upper-limit 0))
[Indentation probably open to improvement]
So, check
is only defined/visible inside of count-check
. count-check
(in turn) is only defined/visible inside of sum-multiples-rec
, and sum-multiples-rec
is only defined/visible inside of sum-multiples
. If (for example) you try to execute something like (check 3)
outside of sum-multiples
, you'll get an error like:
check: undefined; cannot reference an identifier before its definition
This is pretty much the same general idea as (for example) a class in something like Java or C++ making as many if its members private as possible. Limiting the visibility of names dramatically reduces the chances of a name collision that could result in your calling entirely the wrong function. This is particularly important with names like check
that could mean all sorts of different things under different circumstances.
Of course, much like with those other languages, this sort of discipline becomes much more important for larger programs. For a really tiny program, it's not all that big of a deal.
(define (sum-multiples lower-limit upper-limit) (sum-multiples-rec lower-limit upper-limit 0))
. \$\endgroup\$