This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KronixGames's profile picture

TRC - Turtle Remote Control

Started by KronixGames, 18 August 2012 - 12:16 AM
KronixGames #1
Posted 18 August 2012 - 02:16 AM
Hello again folks, here the speaker is the owner of Rednet Works, I'm here again to announce another program Rednet Works, is the CRT = Turtle Remote Control. You will download two programs, the TRC-t and the TRC-c
TRC-c is for computers (consoles)
TRC-t is for Turtles

place the programs in:
TRC-t = .minecraft / saves / (your world) / computer / (your ID turtle)
TRC-c = .minecraft / saves / (your world) / computer / ID (your console)

Download:

http://pastebin.com/nmV1TNeL - TRC-c

http://pastebin.com/U8kX2DBV - TRC-t

and again, sorry for my bad English, is that I'm using "Google Translate"

Enjoy the TRC!

by Works Rednet
BigSHinyToys #2
Posted 18 August 2012 - 03:59 AM
having a function call its self is a really bad thing. using a loop is better example

rednet.open(mSide) -- only have to call this ounce not every time you send a message
function send()
write("Enter the command: ")
cmd = read()
rednet.send(tId, cmd)
print("Sucessfully sended")
end

while true do -- starts loop
send()
end -- end of loop
other that that it is a good turtle remote.
MrBarry #3
Posted 13 September 2012 - 02:46 AM
BigSHinyToys, recursion not necessarily a really bad thing. Your way, which is the way I'd probably do it myself, might have better performance, but I don't know if the difference would be noticible in this case.

matheuscat12, what do you think about adding a modem auto-detect?

local rednetConnect = function()
	for side, label in ipairs(rs.getSides()) do
		--print("Testing " .. label)
		if peripheral.getType(label) == "modem" then
			rednet.open(label)
			return(true)
		end
	end
	--if we get here, there is no modem attached
	return(false)
end
Lyqyd #4
Posted 13 September 2012 - 03:16 AM
BigSHinyToys, recursion not necessarily a really bad thing. Your way, which is the way I'd probably do it myself, might have better performance, but I don't know if the difference would be noticible in this case.

matheuscat12, what do you think about adding a modem auto-detect?

local rednetConnect = function()
	for side, label in ipairs(rs.getSides()) do
		--print("Testing " .. label)
		if peripheral.getType(label) == "modem" then
			rednet.open(label)
			return(true)
		end
	end
	--if we get here, there is no modem attached
	return(false)
end

Yes, the difference would be noticeable in this case and it has nothing to do with the performance. Since it isn't tail-call recursion, each recursion will add to the stack until it overflows.
MrBarry #5
Posted 13 September 2012 - 06:24 PM
Thanks for making me look up "tail-call recursion". It looks like, since cmd is in the global scope, it gets overwritten in each call. Will that still overflow the stack, or is there something else? I'm not super savvy about the stack and how it works and/or is used by lua.
BigSHinyToys #6
Posted 13 September 2012 - 06:50 PM
Thanks for making me look up "tail-call recursion". It looks like, since cmd is in the global scope, it gets overwritten in each call. Will that still overflow the stack, or is there something else? I'm not super savvy about the stack and how it works and/or is used by lua.
Think of a stack of paper. every time a program calls a function a piece of paper is placed in when that function ends its piece of paper is removed and the next function piece is places on top. when a function calls a function then the original function still remains and the new piece goes on top. the paper will stack up till it falls off the desk causing a stack overflow.

a tail call is when a piece of paper swaps with another piece of paper. Its position still remains the same and the stack remains st the same hight.

I am no expert so my understanding or stack could be wrong.

[EDIT]
bellow is better explanation than mine
[/EDIT]
Lyqyd #7
Posted 13 September 2012 - 06:55 PM
The issue is more that every single function must be kept in the stack, since they are all waiting for each subsequent function to return, and none of them ever will. After enough recursions, the stack overflows and it gets dumped. It could be "fixed" with tail call recursion, since Lua actually replaces the current function with the called function in the case of a simple tail call, but using a loop is the correct way to do this. When trying to accomplish looping behavior, a loop is really the correct thing to use.

Edit: To address BigSHinyToys' post, that is a good analogy, but if the tail call also returns anything else, Lua can't replace the position on the stack, so only the simple tail call recursion ('return self()', where self is the name of the function) works in this manner.
MrBarry #8
Posted 13 September 2012 - 07:19 PM
That make sense. This article helped me understand it too. So, if I'm reading this correctly, you could make this a tail call like so?

rednet.open(mSide)
function send()
    write("Enter the command: ")
    cmd = read()
    rednet.send(tId, cmd)
    print("Sucessfully sended")
    return send()
end

Not that you would, but for academic purposes…
Lyqyd #9
Posted 14 September 2012 - 01:07 AM
Yes, that would meet the definition of a proper tail call.