Jump to content
Toggle sidebar
JookWiki
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Navigation
Main page
Recent changes
Random page
All pages
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information
Editing
Recursion
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
More
Read
Edit
Edit source
View history
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==State machines== Not all recursion has to be direct. Indirect recursion lets you represent state machines easily. Here's a tiny adventure game with the player choosing state transitions: function dark_room() print("You are in a dark room.") print("Pick a door: fuzzy or metal") choice = get_choice({fuzzy=1, metal=2}) if choice == 1 then return fuzzy_room() elseif choice == 2 then return metal_room() end end function fuzzy_room() print("This room feels pretty fuzzy...") print("Pick a door: dark, metal") choice = get_choice({dark=1, metal=2}) if choice == 1 then return dark_room() elseif choice == 2 then return metal_room() end end function metal_room() print("This room feels really metallic.") print("Pick a door: dark, fuzzy or win") choice = get_choice({dark=1, fuzzy=2, win=3}) if choice == 1 then return dark_room() elseif choice == 2 then return fuzzy_room() elseif choice == 3 then return metal_room() end end function win_room() print("You found the treasure!") return end dark_room() Without recursion you'd likely need to put everything in a single function with a loop and state variable. Some things just make more sense when implemented recursively, to me at least.
Summary:
Please note that all contributions to JookWiki are considered to be released under the Creative Commons Zero (Public Domain) (see
JookWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To edit this page, please answer the question that appears below (
more info
):
Who owns this wiki?
Cancel
Editing help
(opens in new window)