I am trying to create my own while-loop in racket using the "define-syntax-rule". I want it to be procedural based, so no helper functions (i.e. just using lambda, let, letrec, etc.).
I have this, but it gives me some sort of lambda identifier error.
(define-syntax-rule (while condition body)
(lambda (iterate)
(lambda (condition body) ( (if condition)
body
iterate))))
I want it to be so I can use it like a regular while loop For Example:
(while (x < 10) (+ x 1))
Calling this will(should) return 10 after the loop is done.
How can my code be fixed to do this?
(x < 10)doesn’t make sense, since Scheme uses prefix function application, so it would need to be(< x 10). Second of all, where isxbound? Your current code doesn’t make that clear. Third of all,(+ x 1)doesn’t mutatex, it just produces a new number, just likex + 1would do in a C-like language. You would need to do(set! x (+ x 1))to emulatex += 1. Without those clarifications, it’s hard to answer your question at all, even with your attempted code sample. - Alexis King