Thread: Programming Opinion

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Programming Opinion

    Sooooo... I have a programming 'grade opinion' to ask.

    I hate recursion. I don't get it, don't like it, and hate it. If I have to do it, I'll do it.

    If say.. I have a pretty good grade in my programming class right now - 94% - and I have this stupid recursion project to do (which I don't get) and I can't get it to DO ANYTHING... so basically, if I don't do it, my grade will drop down to a 81%.

    Would you or wouldn't you take this risk of just turning in crap and getting the 0, or actually desperately (and I mean desperate like a person searching for water in a desert) trying to figure out a project (which I don't get)

    Btw.. I still have 1 project & final left and I really want a 'B' in the class.

    Let me know your opinions!

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Why dont you just show us what you've got and ask us for help??

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Definitely go for it. Recursion is a very valuable tool when it comes to programming and while the concept may be a bit abstract at first, it'll click eventually.

    The way I always think about it is when writing recursive functions, you call that function on the assumption that it does what it should, even though right now it may not.

    For example, in implementing the following code:

    Code:
    #include <stdio.h>
    
    int cumulativeSum(int lst[],int size)
    {
        if(!size) return 0;
        else      return lst[0]+cumulativeSum(&lst[1],size-1);
    }
    
    
    
    
    int main()
    {
        int list[5]={1,2,3,4,5};
        printf("%d",cumulativeSum(list,5));
        return 0;
    }
    The code above will output 15, which is the cumulative sum of the list.

    Now, you can easily think of the implementation as saying, add the first element to the sum of the rest of the list. cumulativeSum is supposed to return the sum of a list, so if we pass the rest of the list and add that to the first element's value, we'll get the sum of the entire list. So when writing recursive code, just write it under the premise that it works and does what you think it will, and it'll be much easier.

    Also, think of recursive functions as having a "base case". This base case is the case on which your recursive function stops. In the code above it stops when the size of the list is 0. Therefore we return what is logically the sum of an empty list (0) and then our recursive function automatically unwinds from there.
    Last edited by jverkoey; 11-23-2005 at 03:40 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Jverkoey,

    I understand what recursion is. I can trace-through code, and tell you what it outputs. I just cannot figure out code for myself. btw - I'm the 'recursive word search' post. my recursive word search is my 'doomed project'. Thank you for the encouragement!

    Why dont you just show us what you've got and ask us for help??
    Because, I would have to post my whole (rather embarassing) code and they I would have a bunch of people yelling at me!

    Plus, posting my whole code, in my opinion is cheating. You never learn anything by just posting your whole code, then having someone else debug it for you.

    I think these forums are good for asking hints, or providing little bits and parts of code you don't understand/or have a bug. But posting someone's whole entire programming project is just going too far.

    Other opinions?

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by tofugirl
    I understand what recursion is.
    Sorry for the misunderstanding.

    About your project, sometimes it's good (if time permits) to just try and go at it at a different angle if the current one just plain isn't working. Also, minus the banter in your other post, Quzah did give some good advice on how to implement the code.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Tough it out and get comfortable with recursion, unless you are never going to do anything programming related after this class. Otherwise, you'll suck at writing software.

    If you write recursive functions over and over, you'll get how to do it.

    If you ask me, the best way to think about recursion when writing a recursive function is to think of it as an embodiment (into computer code) of a mathematical relationship. For example, factorial is defined by

    factorial 0 = 1
    factorial n = n * factorial (n - 1)

    and the recursive way of writing factorial is an embodiment of that. (And in the programming language Haskell, what's written above _is_ the recursive way of writing factorial. Hehe.) For another example, the number of nodes in a binary tree follows the mathematical relationship of

    countnodes Null = 0
    countnodes node = countnodes (leftchild node) + 1 + countnodes (rightchild node)

    This is a simple mathematical relationship, and transcribing it directly into C is straightforward:

    Code:
    struct Tree {
      Tree * left, * right;
      int value;
    };
    
    int countnodes(struct Tree * foo) {
      if (foo == NULL)
        return 0;
    
      return countnodes(foo->left) + 1 + countnodes(foo->right);
    }
    Find a mathematical relationship (maybe 'mathematical' here is the wrong word) that describes your word search function, and then you're essentially done.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    About your project, sometimes it's good (if time permits) to just try and go at it at a different angle if the current one just plain isn't working. Also, minus the banter in your other post, Quzah did give some good advice on how to implement the code.
    ok ok. I will try to turn in something. I did what Quzah suggested btw. I just have too many bugs.

    Maybe I'll go at a 'different angle' as you suggest.. then go at a different angle if that doesn't work, etc.

    I'll turn in something that semi-works

  8. #8
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    I also would never ever use recursion, I have never used it and I started programming in 1982. I suggest that you write the recursion function, as they want it, but also create a function that does the same job without recursion, and then show why that is a better and safer solution. That might give you a plus! I would give you a plus if I was your teacher just for saying that recursion is a bad idea from the beginning!
    Good luck! I love that women are learning programming it is needed on this planet!

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by fischerandom
    I also would never ever use recursion, I have never used it and I started programming in 1982.
    I'm almost scared to see the code you've written to traverse the elements of a tree data structure without using recursion.

    It reminds me of the Quzah quote in Thantos' sig: "This is like teaching your dog to bark every time you fall asleep. I mean, sure, you could. But is it really a smart thing to do?"

  10. #10
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Quote Originally Posted by cwr
    I'm almost scared to see the code you've written to traverse the elements of a tree data structure without using recursion.

    It reminds me of the Quzah quote in Thantos' sig: "This is like teaching your dog to bark every time you fall asleep. I mean, sure, you could. But is it really a smart thing to do?"
    I don't care if you are scared about this fact or not. I am working on a Fischerandom Chess program and I don't use any function recursion calling mechanism to traverse trees in the forest there, nor does the best chess programming gurus, those who wrote the chess programs that are the strongest today like Shredder, Junior and Fritz (or Hydra). Do you know why? Of course you don't. Speed is one of the reasons. There is no reason at all to use a recursive function call mechanism traversing a tree, etc.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The only time I've used recursion in the past 20 years is for transversing file systems -- creating a list of all the files in a directory and all its sub-directories. That's probably impossible to do without recursion. The type of programs I write do not need trees -- just simple one and two dimensional lists of objects.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do you know why? Of course you don't. Speed is one of the reasons.
    Please don't assume that you know something we don't unless there's sufficient proof. It makes you sound arrogant and jerkish, especially if you're talking to one of the more knowledgeable people on the forum.

    >I'm almost scared to see the code you've written to traverse the
    >elements of a tree data structure without using recursion.
    Actually, unless it's a quick and dirty traversal, I'll take the time to write a good non-recursive iterator based traversal. The code isn't that scary, and it's more powerful and flexible than a recursive traversal by leaps and bounds.

    >Because, I would have to post my whole (rather embarassing) code
    >and they I would have a bunch of people yelling at me!
    Get over it. Nobody cares if you're embarassed, all we care about is helping you. I've only seen a handful of posts that don't offer constructive criticism on any given code in the years that I've been here, so what you construe as people yelling at you is likely no more than them helping you but eschewing the warm and fuzzies that you seem to want. The people that do that are also usually the best programmers here, and rather than worry about being embarrassed or yelled at, post your code, ask what you think are stupid questions, and learn from them. The only stupid question is the one that you don't ask.
    My best code is written with the delete key.

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I agree with fischerandom, recursion is very rarely a good idea, and we'd probably be better off if we forgot it existed.

    I find it ironic that beginners are taught to use recursion while told never to use goto. Recursion is potentially far more dangerous and there are more legitimate uses of goto than recursion.
    Creating a list of all the files in a directory and all its sub-directories. That's probably impossible to do without recursion.
    Nothing is impossible without recursion. Recursion is just the lazy and dangerous version of a loop with a stack. See this post for an example.
    I'm almost scared to see the code you've written to traverse the elements of a tree data structure without using recursion.
    This is a prime example of when recursion is dangerous. Sure it works in testing with a few deep. A few months later, customers start losing data when they get "out of stack space exceptions".

    In summary, the benefits of using an explicit loop and stack rather than recursion are:
    • Explicit version can fail gracefully, recursion version explodes.
    • Explicit version can use as much memory as available. If it needs more, the stack can even be persisted to hard disk. Recursion version can only use up to the reserved stack memory, 1MB by default on Windows machines (similar on 32bit *nix, I think).
    • Explicit version is faster.

  14. #14
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Quote Originally Posted by Prelude
    >Do you know why? Of course you don't. Speed is one of the reasons.
    Please don't assume that you know something we don't unless there's sufficient proof. It makes you sound arrogant and jerkish, especially if you're talking to one of the more knowledgeable people on the forum.
    Well did you see what cwr wrote to me?
    I'm almost scared to see the code you've written to traverse the elements of a tree data structure without using recursion.

    It reminds me of the Quzah quote in Thantos' sig: "This is like teaching your dog to bark every time you fall asleep. I mean, sure, you could. But is it really a smart thing to do?"

    For this reason I kicked back. And if you read the posts after that you can also see that other, obviously more experienced and knowledgeable people, found that what I said was true. And there is also a difference between assuming and knowing. And I know I am right, he just tried to make me look like a fool out of a horses arse and I don't like such things and I don't like jeallous people either. Some people in here are experienced and can help with technical questions to beginners, but all too often I think they do it in a very arrogant and jerkish way and I am certainly not one of them, besides, I am quite good at C programming after having so many years of experience but I am not behaving bad just because of that. I am a very nice person to those people that behave nice to me and others.
    Last edited by fischerandom; 11-24-2005 at 09:20 AM.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  15. #15
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yes, thinking about it, traversing a tree was a bad example.

    That said, when speed isn't of paramount importance (it often isn't), and there is some arbitrary limit imposed, recursion will suffice.

    It does often result in easier to understand code, which is often more valueable than efficiency. I'm well aware of the disadvantages of recursion that are mentioned above.

    Recursion version can only use up to the reserved stack memory, 1MB by default on Windows machines (similar on 32bit *nix, I think).
    You are right that this is probably generally the case.

    As a matter of interest, Linux will allow the stack to grow just like the heap.

    As an experiment I successfully called a recursive function 60 million times on my linux system without it dying. Granted, it was using 1.7GB of memory and went into swap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-27-2009, 04:43 PM
  2. Her opinion, your opinion
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2002, 10:50 AM
  3. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 07:06 AM
  4. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM
  5. your opinion please
    By iain in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-10-2001, 05:36 AM