#May someone explain this to me?

1 messages · Page 1 of 1 (latest)

solar cairn
#

that's called recursion Thumbs

#

;compile lua lua function f(n) return n>1 and n*f(n-1) or 1 end print(f(5)) -- 120

torn sundialBOT
#
Program Output
120

oak quail
#

Like how

#

like

solar cairn
#

here's another one Doublethumbs

oak quail
#

How does calling the function within the function doesnt create a loop or something

solar cairn
#

;compile lua lua function tpath(t,k,x,...) return x and tpath(t[k],x,...) or t[k] end local t={ a={ b={ c={ d="foo" } } } } print(tpath(t, "a","b","c","d")) -- "foo"

torn sundialBOT
#
Program Output
foo

solar cairn
#

that's why the n>1 and ... or 1 that's what breaks the loop

oak quail
#

Then what stops the loop here

#

And why should i use this technique

solar cairn
#

this is recursing through a chain of objectvalues, so an objectvalue has another objectvalue as its value, and that objectvalue has another objectvalue as its value, repeat until the objectvalue's value is not an objectvalue Thumbs

solar cairn
naive epoch
#

dude I swear there we're just like 3 messages

oak quail
#

And had to know

solar cairn
#

well now you know salute

oak quail
#

I

#

-# guess?

#

still trying to comprehend

#

it

solar cairn
#

the 'opposite' way of writing recursion is an infinite table loop

oak quail
solar cairn
#

;compile lua lua function f(n) local r=1 while n>1 do r=r*n n=n-1 end return r end print(f(5)) -- 120 this is the flat integer

torn sundialBOT
#
Program Output
120

solar cairn
#

the table one is a bit harder

#

;compile lua lua function tpath(t,k,x,...) local varg={...} table.insert(varg,1,x) local r=t[k] while varg[1] do r=r[varg[1]] table.remove(varg,1) end return r end local t={ a={ b={ c={ d="foo" } } } } print(tpath(t, "a","b","c","d")) -- "foo"

torn sundialBOT
#
Program Output
/opt/wandbox/lua-5.4.7/bin/lua: prog.lua:2: attempt to call a nil value (global 'unpack')
stack traceback:
prog.lua:2: in function 'tpath'
prog.lua:11: in main chunk
[C]: in ?

oak quail
#

where the number behind ! gets multiplied by every number behind it except things below 1

torn sundialBOT
#
Program Output
foo

solar cairn
#

there we go

solar cairn
solar cairn
#

vararg

oak quail
solar cairn
#

the symbol itself is called ellipses

oak quail
solar cairn
#

reasonably efficient yes, just need it under like 100 steps otherwise you might run out of stack frames

oak quail
#

like etc. does

sage oasis
solar cairn
#

;compile lua lua function f(n) return f(n) end f(1) -- infinite recursion error

torn sundialBOT
#
oak quail
#

well uh

#

so like

#

its a

#

loop

#

a very

#

wierd loop

#

that has

solar cairn
#

yep

oak quail
#

thats specific

#

like you cant use it for several things

#

like repeat or while or for

#

it has

#

circumstances?

solar cairn
#

anything you can make with recursion can be written using repeat/while, but not for

oak quail
#

well yeah

#

so

solar cairn
#

coz if you could make it with a for loop, you'd use a for loop

oak quail
#

wiat so theres absolutely no reason for me to use recursion

solar cairn
#

well there is kinda

oak quail
#

which is

solar cairn
#

why did you delete original message? i was about to show you the while-loop version of it

oak quail
#

i did?

#

wierd

#

here

solar cairn
#

so the flattened version of this would look something like... lua local function GetTool(tool) while tool and tool:IsA("ObjectValue") do tool=tool.Value end return tool end

#

it does the exact same thing as your function except without the recursion

oak quail
#

That's actually so much easier for me to read

solar cairn
#

sometimes using recursion is much easier to work with than the while loop, but not always Thumbs

oak quail
#

well i guess that answers why people sometimes don't make their code flattened

#

like why for gods sake are you making it so complicated to read when you can keep it simple

solar cairn
#

here's another example of recursion where this really is the best way to write it

#

;compile lua lua function fib(n) return n<2 and n or fib(n-1)+fib(n-2) end print(fib(7))

torn sundialBOT
#
Program Output
13

solar cairn
#

using a while loop isn't super practical for something like this, but interestingly this can be improved with memoization. though i never really bothered learning this far

oak quail
#

Sometimes I'm more fascinated as to how much there is to learn than what I've learnt so far

solar cairn
#
function fastfib(n)
    fibs={[0]=0, 1, 1} -- global variable, outside the function
    for i=3,n do
        fibs[i]=fibs[i-1]+fibs[i-2]
    end
    return fibs[n]
end```
oak quail
#

ok well wrong word

solar cairn
oak quail
#

more like shocked beyond belief

solar cairn
oak quail
#

good advice

#

although

#

it seriously

#

makes me want to

#
while true do
  print("WHAT THE FUCK?!")
end
#

Thanks for all the explaining tho

#

i raelly appriciate that

#

wait

#

okay i tried to do something with the recursion but i probably wrote something thats equivilant to typing "Hi mynajshdjhawhlwdlh, hakjdwij are you?" in English

#

Some things right while the main part is nonsense