Recursive function in bash

There are several syntax and a quite obvious logic one (return 0) A working version is below: #!/bin/bash factorial() { if (( $1 <= 1 )); then echo 1 else last=$(factorial $(( $1 – 1 ))) echo $(( $1 * last )) fi } factorial 5 You are missing: return is bad (should use echo) … Read more

How to predict the maximum call depth of a recursive method?

This is clearly JVM- and possibly also architecture-specific. I’ve measured the following: static int i = 0; public static void rec0() { i++; rec0(); } public static void main(String[] args) { … try { i = 0; rec0(); } catch (StackOverflowError e) { System.out.println(i); } … } using Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java … Read more

SQL Server: How to get all child records given a parent id in a self referencing table

You can try this DECLARE @Table TABLE( ID INT, ParentID INT, NAME VARCHAR(20) ) INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 1, NULL, ‘A’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 2, 1, ‘B-1’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 3, 1, ‘B-2’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 4, 2, ‘C-1’ INSERT INTO @Table (ID,ParentID,[NAME]) SELECT 5, 2, ‘C-2’ DECLARE … Read more

How to avoid property recursion

Best way is to use “Auto implemented properties” here. public int Test { get; set; } If not possible to use “Auto implemented properties” for some reason use _ prefix(I don’t prefer though). If you also don’t prefer to use some prefixes, then you have other option. You don’t have to write the property code … Read more

recursive variadic template to print out the contents of a parameter pack

There’s actually a very elegant way to end the recursion: template <typename Last> std::string type_name () { return std::string(typeid(Last).name()); } template <typename First, typename Second, typename …Rest> std::string type_name () { return std::string(typeid(First).name()) + ” ” + type_name<Second, Rest…>(); } I initially tried template <typename Last> and template <typename First, typename …Rest> but that was … Read more