Lisp: how to write a higher order function

I have this problem to work on:

A more comprehensive procedure of a higher order can be generalized even further to catch the idea of ​​combining terms with a fixed operator. The mathematical product operator is a concrete example of this idea, with multiplication replacing the addition of the sum operator. The accumulation procedure begun below is intended to capture this idea. A combiner parameter is an operator that is used to abbreviate terms, and a base parameter is a value that is returned when there are no remaining conditions for combining. For example, if we had already completed the accumulation procedure, then we could define the summation procedure as:

(determine the amount (copy + 0))

Complete the definition of accumulation so that it is maintained in accordance with this description.

          (define accumulate
             (lambda (combiner base)
                (lambda (term start next stop)
                   (if (> start stop)
                        ...
                        ...))))

I inserted as the last two lines:

   base
   (combiner base (accumulate (combiner start stop) start next stop))

but I have no idea whether this is correct and how to actually use the summation procedure to accumulate and therefore sum the numbers.

+3
source share
1 answer

This is a great way to learn how to fish. Much better than giving fish.

, . , ( + 0). ; defun, , . , , (accumulate * 1) . , . , + *.

, , , . , , , , , (defun accumulate...).

+4

Source: https://habr.com/ru/post/1720146/


All Articles