Looking for recursion Keywords? Try Ask4Keywords

• 1つ以上のベースケース
• 再帰的なステップ

再帰型

T = ... T ...

```data List a = Nil | Cons a (List a)
```

aのリストは、空のリストか、 ' `a` '（リストの "頭"）と別のリスト（ "tail"）を含むコンスセルのいずれかで`a`ことを意味します。

C ++で再帰を伴うパインドメイルのチェック

``````// Checks a string to see if it is a palindrome
bool function IsPalindrome(string input) {
if (input.size() <= 1) {
return true;
}
else if (input[0] == input[input.size() - 1]) {
return IsPalindrome(input.substr(1,input.size() - 2));
}
else {
return false;
}
}
``` ```

再帰を使用して階層構造を作成する[JavaScript]

``````var directories = [
{name: 'users' , parent : null },
{name: 'distalx' , parent : 'users' },
{name: 'guest' , parent : 'users' },
{name: 'shared' , parent : 'users' },
{name: 'documents' , parent : 'distalx' },
{name: 'music' , parent : 'distalx' },
{name: 'desktop' , parent : 'distalx' },
{name: 'javascript' , parent : 'documents' },
{name: 'funjs' , parent : 'documents' },
{name: 'functions' , parent : 'documents' }
]

var sortDirectories= function(directories, parent){
let node = [];
directories
.filter(function(d){ return d.parent === parent})
.forEach(function(d){
var cd = d;
cd.child = sortDirectories(directories, d.name);
return node.push(cd);
})
return node;
}

var results = sortDirectories(directories, null);
JSON.stringify(results, null, ' ');

``` ```

出力

``````
[{
"name": "users",
"parent": null,
"child": [
{ "name": "distalx",
"parent": "users",
"child": [
{ "name": "documents",
"parent": "distalx",
"child": [
{ "name": "javascript",
"parent": "documents",
"child": []
},
{ "name": "funjs",
"parent": "documents",
"child": []
},
{ "name": "functions",
"parent": "documents",
"child": []
}
]
},
{ "name": "music",
"parent": "distalx",
"child": []
},
{ "name": "desktop",
"parent": "distalx",
"child": []
}
]
},
{
"name": "guest",
"parent": "users",
"child": []
},
{
"name": "shared",
"parent": "users",
"child": []
}
]
}]
``` ```

Javascriptでの再帰を使用したフィボナッチシーケンス

``````// Returns the nth number in the Fibonacci sequence
function fibonacci(n) {
if (n === 1 || n === 0) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
``` ```

Javascriptで配列から要素のすべての組み合わせを取得

``````function getCombinations(params, combinationsResults){
if(params.length == 0) return combinationsResults;
var tail = params.slice(1);
var combinationsResultsCurrent = [];
if(combinationsResults.length == 0){
combinationsResultsCurrent.push(item);
} else {
combinationsResults.forEach(function(previousResultItem){
combinationsResultsCurrent.push(previousResultItem.concat([item]));
});
}
});
} else {
if(combinationsResults.length == 0){
} else {
combinationsResults.forEach(function(previousResultItem){
});
}
}
return getCombinations(tail, combinationsResultsCurrent);
``` ```

INカラスを使用したクエリがあるとします。

``````SELECT * FROM custom_table WHERE user_id = 'user1' AND location IN ('home', 'work', AND date IN ('2017-01-10', '2017-01-11'))
``` ```

IN条件なしでクエリを生成するために、パラメータのすべての組み合わせを取得したいとします。

``````SELECT * FROM custom_table WHERE user_id = [value for user_id] AND location = [value for possible location] AND date = [value for possible date]
``` ```

``````var params = ['user1', ['home', 'work'], ['2017-01-10', '2017-01-11']];
``` ```

C / C ++での再帰によるバイナリ検索ツリーの値の検索

バイナリ検索ツリー（BST）ノードの構造：

``````struct node {
int data;
node * left;
node * right;
}
``` ```

``````// Check if a value exists in the tree
bool BSTSearch(node * current, int value)
{
if (current->data == value)
{
return true;
}
else if (current->data < value)
{
if (current->left == NULL)
{
return false;
}
else
{
return BSTSearch(current->left, value);
}
}
else if (current->data > value)
{
if (current->right == NULL)
{
return false;
}
else
{
return BSTSearch(current->right, value);
}
}
}
``` ```