0
(define (teken-n x y n)
  (if (not (zero? n))
      (begin
        (teken-symbolen x y)
        (teken-n x y (- n 1)))))

(define (teken-symbolen-combinatie x y n)
  (if (not (zero? n))
      (begin
        (let ((acc 0))
          (teken-n x y (+ acc 1))
          (newline)
          (teken-symbolen-combinatie x y (- n 1))))))

The use of (teken-n x y n) is to print 2 symbols, 'x 'y in the form xyx n times on 1 line without any spaces. It works correctly.

what I'm trying to accomplish with (teken-symbolen-combinatie x y n) would be the output

(teken-symbolen-combinatie '- '* 3)
-*-
-*--*-
-*--*--*-

My code only gives the first symbol combination, in this example -*- 3 times seperated with a newline. Meaning that my first function's last variable doesn't get incremented correctly. I can't figure out why.

EDIT: correction of mistakes within question

Barmar
  • 741,623
  • 53
  • 500
  • 612
TomatoFarmer
  • 463
  • 3
  • 13

2 Answers2

0

The thing that doesn't get incremented is acc. The function + returns a new value, it doesn't change any of its arguments.

The usual way in Scheme would be to have an inner loop that has another variable (which seems to be what your acc is intended for) increase to n. You do not need to have the outer function loop.

Svante
  • 50,694
  • 11
  • 78
  • 122
0

This might be what you're looking for (not tested; I don't have your other functions)

(define (teken-symbolen-combinatie x y n)
  (define (loop acc)
    (if (not (= acc n))
    (begin
      (teken-n x y (+ acc 1))
      (newline)
      (loop (+ acc 1)))))
  (loop 0))

This uses @Svante's suggestion of creating an inner loop: here it is a recursive function named loop that call itself with an incrementing counter. After its definition, the outer function fires the loop off with an initial value of 0.

This is a pretty common pattern in Scheme: define a function within another that works like a loop by calling itself recursively. If the inner loop is tail-recursive, Scheme will optimize it so it runs fast!

Ashton Wiersdorf
  • 1,865
  • 12
  • 33