So basically this code’s purpose is to simply print out the first n even numbers.
for (i = 0; i <=n; i+= 2)
{
print i;
}
Thing is though, I don’t understand Scheme at all. So, help please.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are several ways to convert the code in the question to Scheme. The first one I can think of:
Notice this:
forloop, I’m using a construct called a named let, which permits the initialization of some iteration variables (iin this case, initialized to0) and the repeated execution of a recursive procedure (loopin this case), producing an effect similar to afor, even in performance(<= i n), when that condition becomes false, the iteration endsbeginsurrounds the body of the “loop”, just as the curly braces{}do in the original codeprintprocedure performs the expected operation; for readability I added a new line after printing each numberi += 2is handled by the expression(+ i 2), inside the recursive callSo you see, the process being executed is essentially the same, only the way to write it (the syntax!) is different. Give it a try, type this:
… And the following will get printed on the screen:
Another possible way to implement the procedure, more similar to the original code, although (this is completely subjective) less idiomatic than the previous one:
Finally, if you’re using Racket this will seem even more familiar to you: