{"id":92,"date":"2007-04-14T15:36:56","date_gmt":"2007-04-14T14:36:56","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=92"},"modified":"2007-04-14T15:36:57","modified_gmt":"2007-04-14T14:36:57","slug":"recursive-programming","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=92","title":{"rendered":"Recursive programming&#8230;"},"content":{"rendered":"<p align=\"left\">If you don&#8217;t know where to start your with your program, <a title=\"Recursion - Wikipedia, the free encyclopedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Recursion\" target=\"_blank\">recursion<\/a> might be an option. I am working on some (perhaps useless) package code to convert an integer into a boolean representation. When I am converting the integer, I don&#8217;t know where to start my conversion. If the integer is relatively small and I start&nbsp;with a&nbsp;high&nbsp;bit&nbsp;(too big) then the overhead is relatively big.<\/p>\n<p align=\"left\">If I start with a smaller bit, I might be missing stuff. Or I will have to do a lot of checking in my code. I always want my computer (or the database) to do the work for me, with as little code as possible. So I want the program to figure out where to start. All I would have to do is provide the starting points and&nbsp;a parameter to catch the return value.<\/p>\n<p align=\"left\">By using recursion in this situation, I can make sure that I will never miss a thing. The idea is checking whether a power of 2 is bigger than the value being converted. If this value is smaller than the to-be-converted-value (tbcv), call the same procedure but with a higher number for the power calculation. When this value is bigger, then add a 0 to the returning string (this will be removed later on). Now, control is being returned to the previous procedure in the call stack. Now we have to check if it&#8217;s possible to subtract the current value from the tbcv without the result becoming below 0. If this is possible, then a 1 is added to the returning string. If it is not possible, then a 0 is added to the returning string. After this control is passed back to the previous procedure in the call stack. And so forth and so on.<\/p>\n<blockquote>\n<p align=\"left\"><font size=\"1\">FUNCTION BINARY(number_in IN PLS_INTEGER<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , positions_in IN PLS_INTEGER DEFAULT NULL )<br \/>&nbsp; RETURN VARCHAR2<br \/>IS<br \/>&nbsp; l_returnvalue bar$util.maxvarchar;<br \/>&nbsp; FUNCTION parseDecimalToBoolean(decimal_in IN VARCHAR2) RETURN VARCHAR2<br \/>&nbsp; IS<br \/>&nbsp;&nbsp;&nbsp; l_decimal_in PLS_INTEGER;<br \/>&nbsp;&nbsp;&nbsp; l_returnvalue bar$util.maxvarchar;<br \/>&nbsp;&nbsp;&nbsp; PROCEDURE addBooleanChar(decimal_in_out IN OUT NOCOPY PLS_INTEGER<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , power_in IN PLS_INTEGER<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , booleanchar_in_out IN OUT NOCOPY VARCHAR2)<br \/>&nbsp;&nbsp;&nbsp; IS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l_powered PLS_INTEGER;<br \/>&nbsp;&nbsp;&nbsp; BEGIN<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l_powered := power(2, power_in);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF l_powered &lt; decimal_in_out THEN<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addBooleanChar(decimal_in_out, (power_in + 1), booleanchar_in_out);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END IF;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF l_powered &gt; decimal_in_out THEN<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; booleanchar_in_out := booleanchar_in_out || &#8216;0&#8217;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; booleanchar_in_out := booleanchar_in_out || &#8216;1&#8217;;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; decimal_in_out := decimal_in_out &#8211; l_powered;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END IF;<br \/>&nbsp;&nbsp;&nbsp; END addBooleanChar;<br \/>&nbsp; BEGIN<br \/>&nbsp;&nbsp;&nbsp; l_returnvalue := &#8221;;<br \/>&nbsp;&nbsp;&nbsp; l_decimal_in := decimal_in;<br \/>&nbsp;&nbsp;&nbsp; addBooleanChar(l_decimal_in, 0, l_returnvalue);<br \/>&nbsp;&nbsp;&nbsp; &#8212; remove any leading zero&#8217;s (if they&#8217;re there)<br \/>&nbsp;&nbsp;&nbsp; l_returnvalue := ltrim(l_returnvalue, &#8216;0&#8217;);<br \/>&nbsp;&nbsp;&nbsp; RETURN l_returnvalue;<br \/>&nbsp; END parseDecimalToBoolean;<br \/>BEGIN<br \/>&nbsp; IF number_in IS NULL THEN<br \/>&nbsp;&nbsp;&nbsp; l_returnvalue := NULL;<br \/>&nbsp; ELSE<br \/>&nbsp;&nbsp;&nbsp; IF number_in = 0 THEN<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l_returnvalue := &#8216;0&#8217;;<br \/>&nbsp;&nbsp;&nbsp; ELSE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l_returnvalue := parsedecimaltoboolean(number_in);<br \/>&nbsp;&nbsp;&nbsp; END IF;<br \/>&nbsp;&nbsp;&nbsp; IF positions_in IS NOT NULL THEN<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l_returnvalue := lpad(l_returnvalue, positions_in, &#8216;0&#8217;);<br \/>&nbsp;&nbsp;&nbsp; END IF;<br \/>&nbsp; END IF;<br \/>&nbsp; RETURN l_returnvalue;<br \/>END BINARY;<\/font><\/p>\n<\/blockquote>\n<p align=\"left\">I haven&#8217;t built enough <a title=\"Test case - Wikipedia, the free encyclopedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Test_case\" target=\"_blank\">test cases<\/a> in <a title=\"Quest Code Tester for Oracle\" href=\"http:\/\/unittest.inside.quest.com\/\" target=\"_blank\">CodeTester<\/a>&nbsp;yet, but with the 20 or so I have, it runs successfully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you don&#8217;t know where to start your with your program, recursion might be an option. I am working on some (perhaps useless) package code to convert an integer into a boolean representation. When I am converting the integer, I don&#8217;t know where to start my conversion. If the integer is relatively small and I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,2,5],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-codetester","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}