Search code examples
schemesicp

Understanding of the part of the code


Please, can you explain to me what the part of if (the one that is noted with ***) is doing? The explanation given was that it adds conditional to the (term a). However, I think in that case there must be written something like:

 (if (filter term a)

The whole code is below:

 (define (filtered-accumulate combiner null-value term a next b filter) 
    (if (> a b) 
   null-value 
   (combiner (***if (filter a) 
                 (term a) 
                 null-value)*** 
             (filtered-accumulate combiner null-value term (next a) next b filter)))) 

Solution

  • It's if and it pretty much work the same way as if in other languages except that it always evaluates to a value. So (filter a) is evaluated and if it is a true value (thus anything other than #f) the result of the if is the result of (term a). Otherwise the evaluation of the variable null-value will be the result. Since this is the expression in argument position of the form (combiner ...) that value will become the first argument in that form.

    Both combiner and filter are just variables the evaluate to whatever they were passed and from the code to work they must be functions, but their names do not dictate how they are used. I have no idea if filter takes one or two arguments. It might be that the name indicates that this is a filter predicate and then it should only take one argument. Again, educated guess only and I might be wrong.