Depending on the Scheme interpreter in use, there are several ways to implement the required loops. For example, in Racket it's as simple as using iterations and comprehensions:
(define (orderedTriples n)
(for* ([i (in-range n -1 -1)]
[j (in-range n -1 -1)]
[k (in-range n -1 -1)])
(printf "(~a, ~a, ~a)" i j k)))
The style of programming shown in the question (assuming it worked) is heavily discouraged in Scheme - using mutation (the set!
operation) for looping is a big no-no, that's how you'd solve the problem in a C-like language, but in Scheme in particular (and in Lisp in general) there are other constructs for implementing iteration in a program (the solution given by @TerjeD demonstrates the use of do
, for instance), and even if such constructs didn't exist, a recursive solution or a solution using higher-order procedures would be preferred. For example, here's another possible solution, using nested mappings with only standard procedures (with the exception of printf
, which is non-standard):
(define (range n)
(if (negative? n)
'()
(cons n (range (- n 1)))))
(define (orderedTriples n)
(for-each (lambda (i)
(for-each (lambda (j)
(for-each (lambda (k)
(printf "(~a, ~a, ~a)" i j k))
(range n)))
(range n)))
(range n)))