You do need to understand recursion, and simple contrived examples are a way to introduce the learner to them. You don't immediately want to jump into a quicksort implementation or a tree visitor or anything like that.
No, not really. For the given task, this is an appropriate solution. (I still use recursion, if it is not too expensive and if it is more elegant than an iteration.)
Please read the task again. The teacher explicity asks for a recursive solution.
I agree, that a for loop would be better in this case. However, the speed doesn't matter because not much data is processed here. The recursive method / function is also easy to read, if you have a basic understanding of how recursion works.
Intentionally bad code is against rule 1 of the sub. Code that is used for educational purposes to teach a concept may be "bad" in the sense that it is inefficient and would never be used in the "real world", but it is intentionally so. Plus I'd argue that the primary purpose of this code is education. The fact that the principles or recursion are being taught through printing out the first n numbers is irrelevant
This is perfectly normal for learning how to use recursion. Sure it's not how you should do this, but I'd bet everyone in your class is glad that they're not starting out with quicksort
What kind of things are you programming? While not as common as uni might have led me to believe I still think recursive functions are an essential tool that I use daily.
I find that there are plenty of algorithms that are much cleaner and much easier to understand when written recursively, especially when working with data structures that are defined recursively.
Yes you could re-write almost any recursive function using a queue or a while loop but unless there is a clear performance benefit I would much rather make my code clear and easy to understand.
I think many problems that can be solved with recursion can also be solved with iteration, which is more intuitive in most cases
I've actually had a couple of problems I needed to solve with recursion (traversing folder trees, raycasts with reflection), but I'm under the impression that if your code is clean, your recursion logic could also be written iterative
that being said, in some cases recursion can lead to more readable code
I would go a step further and claim that all recursive algorithms can be written iteratively and vice versa. I'm pretty sure this has a mathematical proof. As you say, recursion can be very clear in some situations.
In computability theory, the Ackermann function, named after Wilhelm Ackermann, is one of the simplest and earliest-discovered examples of a total computable function that is not primitive recursive. All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are primitive recursive. After Ackermann's publication of his function (which had three nonnegative integer arguments), many authors modified it to suit various purposes, so that today "the Ackermann function" may refer to any of numerous variants of the original function.
It's very useful for writing immutable functional code. But for it to be truly useful you need tail call compiler optimization or you will get a stack overflow.
I used recursion yesterday because I needed something that would apply a function to the last level of key-value pairs of a nested dictionary with unknown depth. It's problems like that where recursion shines. When you're dealing with flatter structures like simple sequences, which is much more common, iteration is simpler.
I think it's a reasonable solution. The assignment was "print the first n natural number", so it must always begin by printing 1. This solution ensures that will be the case, and that the numbers will be printed in sequence until it reaches the number passed in the original call. It pushes calls onto the stack beginning with 10 and descending to 1, and then prints them as it pops them off the stack.
Doing it in the other order would be possible, but it would have required the first call to pass a 1, the number to be printed before the recursive call, and it would have required the upper limit rather than the lower limit to be persistent; i.e., "if (n < 10) NaturalNumber(n + 1);". However, since the instructions explicitly said "first n natural number", and since the quantity of numbers to print was specified as a parameter, the descending order recursion makes more sense.
It's simple, it's elegant, and it does exactly what the instructor asked for.
The code isn't bad, the question is. I guess it sort of doesn't belong here, which is why it's the second most controversial post of all time here with a 59% upvote/downvote ratio
Think of it as a math/problem solving question, not a “coding” question. The natural numbers are axiomatically defined with something like “recursion”, the question just helps you to see that
This example, while pretty silly from a software engineering point of view is interesting because it illustrates the connection between recursion and mathematical induction.
Although wouldn't the output not match what's expected, since it's going to write each number on a new line, instead of all on the same line?
I might also argue that it'd be better to have the NaturalNumber function return either the string of natural numbers, or an iterable that can be joined and printed in Main.
The return type is void. In c# you don't need to explicitly return null, as the compiler will automatically imply that when the end of the method block is reached.
102
u/wknight8111 Dec 08 '21
You do need to understand recursion, and simple contrived examples are a way to introduce the learner to them. You don't immediately want to jump into a quicksort implementation or a tree visitor or anything like that.
9
u/circuit10 Dec 08 '21
Text:
4. Write c# programs:
Test data:
How many numbers to print : 10
Expected output:
1 2 3 4 5 6 7 8 9 10
7
u/blehmann1 depraved Dec 08 '21
This is perfectly normal for learning how to use recursion. Sure it's not how you should do this, but I'd bet everyone in your class is glad that they're not starting out with quicksort
37
u/jonnysteps Dec 08 '21
C Sharp Sharp??
16
u/Shad_Amethyst Dec 08 '21
Is there TCR optimization in C#? In which case you could've made a recursive function that's just as fast as a loop
13
u/wiliamks Dec 08 '21
for some reason, schools love recursion, I've never used it outside of school tho
4
u/[deleted] Dec 08 '21
The only bad thing about this code I can find is naming the method "NaturalNumber". PrintNumber() would be my choice.
Recursion has its place, and it's definitely not here. But the code itself isn't bad.
4
u/Bo_Jim Dec 08 '21
I think it's a reasonable solution. The assignment was "print the first n natural number", so it must always begin by printing 1. This solution ensures that will be the case, and that the numbers will be printed in sequence until it reaches the number passed in the original call. It pushes calls onto the stack beginning with 10 and descending to 1, and then prints them as it pops them off the stack.
Doing it in the other order would be possible, but it would have required the first call to pass a 1, the number to be printed before the recursive call, and it would have required the upper limit rather than the lower limit to be persistent; i.e., "if (n < 10) NaturalNumber(n + 1);". However, since the instructions explicitly said "first n natural number", and since the quantity of numbers to print was specified as a parameter, the descending order recursion makes more sense.
It's simple, it's elegant, and it does exactly what the instructor asked for.
1
u/[deleted] Dec 28 '21
I'm scared to say it, but I don't know what about this code is bad. Can someone point it out to me?
3
u/paplike Dec 08 '21
Think of it as a math/problem solving question, not a “coding” question. The natural numbers are axiomatically defined with something like “recursion”, the question just helps you to see that
1
u/ONLYUSEmyTOILET Dec 08 '21
Aside from the content of this function, is using recursion without branching conaisdered good practice?
1
u/codeguru42 Dec 09 '21
This example, while pretty silly from a software engineering point of view is interesting because it illustrates the connection between recursion and mathematical induction.
1
u/thefriedel Dec 23 '21
The Final Boss: C Sharp Sharp
0
u/zacharypamela Dec 08 '21
Although wouldn't the output not match what's expected, since it's going to write each number on a new line, instead of all on the same line?
I might also argue that it'd be better to have the
NaturalNumberfunction return either the string of natural numbers, or an iterable that can be joined and printed inMain.-15
u/circuit10 Dec 08 '21
Obviously the question is for learning purposes but it would be bad to actually use that for anything
0
u/[deleted] Dec 08 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 16 '22
[deleted] — view deleted comment