(define (orderedTriples n)
(set! i n)
(set! j n)
(set! k n)
(while (>= i 0)
(while (>= j 0)
(while (>= k 0)
(printf "(~a, ~a, ~a)" i j k)
(set! k (- k 1)))
(set! j (- j 1)))
(set! i (- i 1))))
So my issue is…I am confused as to how to make while loops work in scheme (I’m very new to this so excuse the syntax if I am WAY off). I typed while into here just for the purpose of working through a problem and to show what I am trying to accomplish. Could anyone help me with a simple recursion example or nested recursion?
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:
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 ofdo, 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 ofprintf, which is non-standard):