Reputation: 223
I wish to write a simple procedure in the Logo programming language (Turtle), which does this process a number of times (the number of times can be an input of the procedure): generate a random number between 0 to 10. If the number is below 5, make the turtle red, if above/equal make it blue. In addition, I want to count the number of times the turtle was blue and to return the probability of a blue turtle.
My problem is mainly in assigning values to variables. I don't know how to create a counting variable in Logo and couldn't find it anywhere online. The part of counter = counter + 1, how do I do that in Logo?
Any help with this simple friendly procedure will be most appreciated ! :-)
Thank you.
Upvotes: 1
Views: 257
Reputation: 41903
I don't know how to create a counting variable in Logo and couldn't find it anywhere online. The part of counter = counter + 1, how do I do that in Logo?
A common approach involves recursion:
to count :counter
if :counter = 0 [STOP]
pr :counter
count :counter - 1
end
count 10
If you want to decrement (or increment) the counter directly, you can instead do:
to count :counter
if :counter = 0 [STOP]
pr :counter
make "counter :counter - 1
count :counter
end
count 10
I'm guessing Logo relies on tail recursion to optimize this approach to be as efficient as iteration.
Upvotes: 0