Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 
What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 
added 427 characters in body
Source Link
TacticalCoder
  • 6.3k
  • 3
  • 34
  • 39

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them just "cheat" by using the language's max facility but other implementation, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them just "cheat" by using the language's max facility but other implementation, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 

what is the value on result

Well that's the point of recursion: there's not one value of result, which may be what is confusing you. Result's value is, in order, 1, 1, 1 then 2.

What is the steps here when we running this method?

Here's what happens:

What is the max of: {1} ? Result is: 1 What is the max of: {(1), 1} ? Result is: 1 What is the max of: {(1, 1), 0} ? Result is: 1 What is the max of: {(1, 1, 0), 2} ? Result is: 2 

Note that debugging is indeed not that trivial when working with recursive methods. It sometimes helps much more to print the method calls "indented" to the depth recursion level, like in this question I answered here:

How do I solve the 'classic' knapsack algorithm recursively?

(of course you need to somehow keep track of the 'depth' of the recursive calls)

and what is the role of (a, length-1) ? (i've tried the debugger but it's not helped me)

The debugger may not be helping you because that method is using a 'trick' in order to save memory in Java: basically the input is shortened by indicating that you shouldn't parse the entire array. But you're still recursively passing the entire array to the method. This is a "poor man's functional way of getting a list with one less element" ; )

Here's a link showing how to find the maximum value of a list in a lot of different language. Some of them, like the OCaml one, are expressed in a functional form:

http://rosettacode.org/wiki/Greatest_element_of_a_list

Three lines of OCaml (source: Wikipedia):

let my_max = function [] -> invalid_arg "empty list" | x::xs -> List.fold_left max x xs 
Source Link
TacticalCoder
  • 6.3k
  • 3
  • 34
  • 39
Loading