-1

I want to find a way to use a counter within a function where the counter is also a variable within the recursive function. An example of this is with a program that takes a list and then "sifts" through it until it finds all the numbers within it that are multiples of i:

(define (multiples-of lst) (lambda (i) (if (> i 3))
                               '()
                               (multiplefilter (ismultipleof (+ i 1)) (lst)))))

where ismultipleof checks if the car of each list is a multiple of i + 1 (with i starting at 1) and then the multiplefilter is a separate function that scraps any values of the list that are not multiples of i. So that if I put in the list (1 2 3 4 5 6 7 8 9 10 11 12) the output would just be (6 12). The biggest issue is getting said counter to work within the function.

  • I've tried using (cond as well in place of the if statement but I still get the same issue of it not working. – Apeirologue Mar 01 '21 at 07:08
  • It looks like the way you are defining functions is a little messed up. Could you include an example of how you want to call this procedure? – clartaq Mar 01 '21 at 15:06
  • Could you add a little clarification? The question references three functions: `multiplesof`, `multiplefilter`, and `multipleof` (no 's'). Is that a typo or what you want? – clartaq Mar 01 '21 at 15:56
  • Also, as I understand the question, `multipleof` is a predicate that would return #t for multiples of 4 in your example, i.e `'(4 8 12)`. If that is true, it seems like your example should return just `'(12)`. Again, is that what you intend? – clartaq Mar 01 '21 at 16:00
  • the names are a bit messy but that is the intention. The way I wanted to numbers to work would be a loop (so check if the list is multiples of 1, 2, and 3. I'll adjust the code to make it easier to read. I would be calling it either through a different function or by itself, right now I'm just having trouble with the loop aspect of it. – Apeirologue Mar 01 '21 at 20:28
  • [mcve], please. otherwise it is impossible to understand. for a possibly related example of using the let-over-lambda technique, see `not-divisible-by` [here](https://stackoverflow.com/a/66340403/849891). – Will Ness Mar 04 '21 at 16:35

1 Answers1

0

Well, here's a simple function that uses a counter in its execution. range just returns a list of integers from 0 up to (- n 1). It's just as easy to write something that increments a counter.

(define (range n)
  (let loop ((acc '())
             (list-start (- n 1)))
    (if (negative? list-start)
        acc
        (loop (cons list-start acc) (- list-start 1)))))

This function counts down rather than up because recursion usually builds lists "backwards". Counting down just avoids the need call reverse on the acc when the recursion is finished.

Iteration is usually accomplished with recursion in Scheme instead of for loops as in some languages. In particular, recursion from the tail is something to strive for since it will not "blow the stack".

clartaq
  • 5,320
  • 3
  • 39
  • 49