how can I understand recursion?
I thought that I understand recursion in C ....but not in Javascript
Which book or video should I see or read to understand this recursion mess??
![]() UFOs Are A Psyop Shirt $21.68 |
![]() DMT Has Friends For Me Shirt $21.68 |
![]() UFOs Are A Psyop Shirt $21.68 |
My first thought was to tell you to have a nice day. I am trying to stop being nasty and bothered by it but it's so painful to see the only Internet community where I manage to get enjoyment out of posting to get shitposts like this that will get 200 replies.
Lol, I feel you. At least your post made me smile so the thread wasn't a complete waste.
>get 200 replies
why?
Because it gives an opportunity for morons who took their first programming class 1 week ago to argue and feel smug by explaining recursion.
AND it has a provocative image.
If you know how IQfy works you know this is a recipe for a thread with a lot of replies. Not 300 because only current events and generals get that many replies before people get bored of it.
There's a hide button. Besides, this is hardly the worst post.
It's a function that, in it's definition, calls itself until it checks if it's done and exits, usually they're stupid math dickwaving but they make sense for some kinds of problems, usually traversing directory structures, tree data structures, anything that looks like a branching tree.
make it sound sound, but with code it becomes very confusing
suicide is kinda a recursion thing ?
No they never make sense. All recursive functions crash.
how can I understand recursion?
I thought that I understand recursion in C ....but not in Javascript
Which book or video should I see or read to understand this recursion mess??
i hate these new languages, all looks so messy...specially this zommer thing called javascript...it makes me angry, i wanna beat the shit out of my wife everytime i have to remember all this weird syntax...in my times, we use C, it was clean and smooth, it was perfect
> C, it was clean and smooth, it was perfect
really clean and smooth frick you are delusional c code is fricking giberish.If its so clean and smooth go teach a newbie c in 2 days .Guess what you cant.Lisp is clean and smooth c syintax is horrible.
All that you need to know about recursion is that it will always crash your computer. Never do it, and force every single person who does do it to make a proper algorithm with a while or for function.
tco
If you understand recursion in one language, you understand it in all of them. All it is, is a function calling itself. This causes it to repeat every part of the function that comes before the recursive call, as if you had constructed a while loop. Ideally, you would use something like an if statement to make the recursive call not be executed every time, so you could eventually terminate the loop. Also, you should probably want to minimize the number of times you recurse, otherwise you'll end up overflowing the stack (unless your language in question can perform an optimization to the contrary, like tail call optimization), but fundamentally it's just another way to do looping.
wait...stack ..overflow ..is not a website?
What behavior do you think the website was named after?
Please refrain from taking drugs right before posting on IQfy.
recursion may not be a loop in the sense that it is not a factorial, because recursion is multidimentional, and factorials are not multidimentional?
thnks ..this is awesome
u have to turn off your brain and assume the recursive call works
Ah, yes, the "leap of faith" technique.
100% OP please read SICP, the first and second chapter really touch on the difference between recursion and iteration. There's also conveniently a JavaScript version of the book.
Javascript version of SICP? Do tell more.
/thread
Recursion has (kind of) nothing to do with the language you use. The key to it is that you're doing the same thing over and over, usually with a number that gets smaller until you hit a base case. Take factorials:
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 <- base case
If you wanted to implement this in python, it would look something like
def fac(n: int) -> int:
return (1 if n == 1 else n * fac(n - 1))
So when you run fac(5), the function calls fac(4) and so on. One weakness is that if you input a negative number, the base case will never be reached, and you'll be stuck in an infinite loop.p
>One weakness is that if you input a negative number, the base case will never be reached, and you'll be stuck in an infinite loop
Because you wrote your base case poorly. Actually, not only are you going to get an infinite loop on negative numbers, but on 0 as well. And yet 0! is well defined to be 1.
it's like a proof by induction but backwards
Put your entire method in a loop that counts down, the stop condition is that it hits the lowest index or otherwise can't continue. That's 90% of recursion algorithms
recursion is a subsect of loops, where loops are ran until a condition is met, recursion is ran until all elements have been touched. a "for x in y" loop may seem like recursion, but this loop can be programmatically stopped with a "break" statement which immediately returns False to the loop. recursion doesn't care about conditionals like that. the classic "rm -rf" uses "r" to indicate recursion, and this makes the remove command run against every element available (every file in the directory).
break does not return false to a loop.
It initiates an unconditional jump to the outside part of the loop.
>recursion is a subsect of loops
no it isn't
recursion is actually more powerful than loops, there is no simple way to express many simple recursion schemes in terms of loops
>where loops are ran until a condition is met, recursion is ran until all elements have been touched
not the case. you can exit out of recursion any point you want. you don't have to recurse over some elements of a collection either.
hylo :: (a -> b -> (b,a)) -> (a -> Bool) -> a -> b -> b
hylo f stop x acc = if stop x then acc else let (acc',x') = f x acc in hylo f stop x' acc'
is one way of encoding a hylomorphism, there is a function called stop (often integrated into the generating function f by replacing its type with a -> b -> Maybe (b,a)) which we literally check the iteration variables x and acc and stop recursing returning acc if that condition holds.
A method that calls itself.
Everytime the method calls itself, the previous instance of itself has not yet been completed.
So all recursive methods can use infinite amounts of data and crash your computer, making them pure trash.
Why yes, i'm a ASM GOD from 1996, how could you tell?
>wtf is recursion?
The great filter.
Worse than for loops and that should be enough to tell you to stay away.
Incoming recursion from Hell:
let insert cmp l x =
let rec loop rl = function
| [] -> List.rev_append rl [x]
| y :: ys ->
if cmp x y <= 0 then
List.rev_append rl (x :: y :: ys)
else
loop (y :: rl) ys in
loop [] l
;;
let mk_rand_list len =
let rec loop l = function
| 0 -> l
| n -> loop (Random.int 100 :: l) (pred n) in
loop [] len
;;
let sort_by_insertion cmp l =
let insert = insert cmp in
List.fold_left insert [] l
;;
let print_list pp oc l =
Printf.fprintf
oc "[%t]"
(fun oc ->
match l with
| [] -> ()
| x :: xs ->
Printf.fprintf
oc "%a%t" pp x
(fun oc ->
List.iter (fun x -> Printf.fprintf oc ",%a" pp x) xs))
;;
let print_int oc = Printf.fprintf oc "%d";;
let dump = Printf.fprintf stdout "%an" (print_list print_int);;
let main () =
let l = mk_rand_list 20 in
dump l;
let l = sort_by_insertion ( - ) l in
dump l;
flush stdout
;;
let () = main ();;
https://haskell.mooc.fi/
OP, the pic is misleading you, that's more of an infinite loop
>to understand recursion, you musts...
dont bother, problably just learn in for stupid leetcode shit at job interviews, literally 0 use on real world.
i remember doing complex deep recursions that loops through 5 tables before and i have never used it in real production applications
Step 1: understand recursion is a terrible idea
What do you mean you dont understand recursion? Its just a function that ends up calling itself, directly or indirectly.
something that nerds use cause they're too good to use while loops like a normal person
i know recursion but cant implement one in frontend
>how can I understand recursion?
Use a stack like structure and trace it through you fricking spastic.
If you're not even using the outcomes of a previous iteration for the next one, then plain loops have shit to do with recursion.