From: mental@... Date: 2005-12-13T05:05:03+09:00 Subject: Re: A question about recursive programming Quoting Hank Gong : > You program works. It was just intended to illustrate a trivial transformation from iterative to recursive. > Mine one listed here: > > def max(arr) > case arr.size > when 1 then return arr.first #<---Wrong > when arr.first>max(arr[1..-1]) then return arr.first > else return max(arr[1..-1]) > end > end That's a Ruby problem, not a recursion problem; you can't mix forms of case/when like that. There's a choice of either: case when [boolean condition] ... end or: case value when [other value to compare with ===] ... end If you rewrite this using the first form: def max( arr ) case when arr.size == 1 then return arr.first when arr.first > max( arr[1..-1] ) then return arr.first else return max( arr[1..-1] ) end end It will work, although it's not optimal. For one thing, return is unnecessary here, and more importantly max( arr[1..-1] ) will be called twice needlessly... -mental