none
ConsList Recursion

    Question

  • I need to write a method with return type ConsList<int>, and two parameters "ConsList<int> list" and "out int min". This method must use recursion to return the result of removing one occcurence of the smallest element in list and assign min to the value which is removed. Can anyone help me out?
    Tuesday, February 17, 2009 8:37 PM

Answers

  • Do something like this:

    private ConsList < int> recursiveMethod ( ConsList<int> list, out int min )
    {
        min = 100000;
        int[] intArr = list.ToArray ( );
        foreach ( int member in intArr )
        {
            min = Math.Min ( min, member );
        }
        // what if there are two equal min values or more? Let's assume there is only one.
    }

    Well, I was about to finish it off but then I realized that you are asking me to do your school assignment. You better finish it off yourselves.

    AlexB
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:08 PM
  •  Hi,

    What is the point of this method? What is the point of the recursion? If you just want a list without the smallest member do something like this:

    private List<int> removeSmallest(List<int> list, out int min)

    {

    list.Sort();

    min = list[0];

    list.RemoveAt(0);

    return list;

    }

    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:37 PM
  • As a tutor, your reply to the student should have been that this is not an appropriate method where recursion would be handy. In fact I can't think of one reason anyone'd want to write a recursive function for this problem.

    http://blog.voidnish.com
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:38 PM
    Moderator
  • This teacher seems pretty confused between object oriented programming and functional programming.  "cons" is a very functional word.  Teaching students functional concepts and asking them to write them in C# seems pretty dodgy.  C# is an object oriented language.  The teacher would do much better teaching these functional concepts in F#, OCaml or some other functional language, nevertheless, there's no way to know what exactly to do without seeing the ConsList<> class.


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:52 PM
    Moderator

All replies

  • Do something like this:

    private ConsList < int> recursiveMethod ( ConsList<int> list, out int min )
    {
        min = 100000;
        int[] intArr = list.ToArray ( );
        foreach ( int member in intArr )
        {
            min = Math.Min ( min, member );
        }
        // what if there are two equal min values or more? Let's assume there is only one.
    }

    Well, I was about to finish it off but then I realized that you are asking me to do your school assignment. You better finish it off yourselves.

    AlexB
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:08 PM
  • Im a tutor and a student asked me this question the other day, and I was unable to answer. But your answer doesn't use recursion anyway, loops are not allowed.

    Tuesday, February 17, 2009 10:17 PM
  •  Hi,

    What is the point of this method? What is the point of the recursion? If you just want a list without the smallest member do something like this:

    private List<int> removeSmallest(List<int> list, out int min)

    {

    list.Sort();

    min = list[0];

    list.RemoveAt(0);

    return list;

    }

    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:37 PM
  • As a tutor, your reply to the student should have been that this is not an appropriate method where recursion would be handy. In fact I can't think of one reason anyone'd want to write a recursive function for this problem.

    http://blog.voidnish.com
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:38 PM
    Moderator
  • The assignment was given to practice recursion, and projects which did not implement recursion would result in loss of points.
    Tuesday, February 17, 2009 10:39 PM
  • I googled to see if ConsList is some specific type of data structure where this recursion would make sense. But there are no results on Google. I did find one reference to it where someone else is trying to write a recursive method (in a different forum). Interestingly that thread was posted today too.

    Is this some specific list-derived class that you guys use in your class assignment?

    http://blog.voidnish.com
    Tuesday, February 17, 2009 10:43 PM
    Moderator
  • Yea ConsList is a type of list which I guess is meant to be used with recursion, or atleast the teacher calls it a "recursive type". Recursion is sort of a tough concept to understand though. If you can help me out let me know.
    Tuesday, February 17, 2009 10:49 PM
  • This teacher seems pretty confused between object oriented programming and functional programming.  "cons" is a very functional word.  Teaching students functional concepts and asking them to write them in C# seems pretty dodgy.  C# is an object oriented language.  The teacher would do much better teaching these functional concepts in F#, OCaml or some other functional language, nevertheless, there's no way to know what exactly to do without seeing the ConsList<> class.


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Monday, February 23, 2009 2:30 AM
    Tuesday, February 17, 2009 10:52 PM
    Moderator
  • Can you post the definition of the ConsList class? 
    http://blog.voidnish.com
    Tuesday, February 17, 2009 10:53 PM
    Moderator