Metric space: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Tobias Bergemann
Undid revision 587213715 by Lolmid (talk)
en>Harryalerta
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
In [[mathematics]] and [[computer science]], '''mutual recursion''' is a form of [[recursion]] where two mathematical or computational objects, such as functions or data types, are defined in terms of each other.<ref>Manuel Rubio-Sánchez, Jaime Urquiza-Fuentes,Cristóbal Pareja-Flores (2002), 'A Gentle Introduction to Mutual Recursion', Proceedings of the 13th annual conference on Innovation and technology in computer science education, June 30–July 2, 2008, Madrid, Spain.</ref> Mutual recursion is very common in [[functional programming]] and in some problem domains, such as [[recursive descent parser]]s, where the data types are naturally mutually recursive, but is uncommon in other domains.
== Chixuedongfu北築山、秦ゆう正ボレースタンド ==


==Examples==
があり、スピーカー、彼は緑のヒョンチョンチョンは彼に近いLaoguiとチャールズハング強度が気にしないだろうと述べていることを聞いたからです。<br>現時点秦ゆうで<br>、侯飛、三兄弟心に黒い羽は、一般的な文字は、彼らに脅威がなく、今では最大の敵は、残りの9 [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_13.php クリスチャンルブタン通販] 8悪悪紫龍の寺院です。 「ブラック、あなたは退却を行く元英を洗練開始しました。その後、いくつかの私たちの生活の中での時間とよりエキサイティングが起こるのだろう、低強度は、そのフィールドのイベントに参加する資格がないかもしれません。 '秦ゆう口笑顔で、目Limang光った。秦三兄弟、両方の将軍秦。今日の皇帝秦鄭は、時間までの冷酷なわずかな慈悲がないでしょう。秦Yuは彼が今考慮し、最悪の場合、秦ゆう、何を恐れて考えられ、最悪の場合を取り、する必要のあるタイムに関係なく、また、将来の期待、の場合ではありません。ブラックフェザー。弱いなしの侯飛、あらゆる混練柿。 [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_13.php クリスチャンルブタン通販] 「私たちは自由に三兄弟をこねるしたい、私たちは彼の手が切断されたことを注意しなければならない。 [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_14.php クリスチャンルブタン メンズ 靴] '秦Yuは外部に見える。<br><br> [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_9.php クリスチャンルブタン メンズ 通販]......<br>次<br>Chixuedongfu北築山、秦ゆう正ボレースタンド [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_12.php 靴 クリスチャンルブタン]。
相关的主题文章:
<ul>
 
  <li>[http://dgxcxx.com/plus/feedback.php?aid=118 http://dgxcxx.com/plus/feedback.php?aid=118]</li>
 
  <li>[http://www.adsrack.com/article/56274/minnesota-state-remains/index.cgi http://www.adsrack.com/article/56274/minnesota-state-remains/index.cgi]</li>
 
  <li>[http://www.zpr8.com/thread-26611-1-1.html http://www.zpr8.com/thread-26611-1-1.html]</li>
 
</ul>


===Data types===
== は '周フオは、これはあなたの土地の力とは ==
{{further|Recursive data type}}
The most important basic example of a data type that can be defined by mutual recursion is a [[tree (data structure)|tree]], which can be defined mutually recursively in terms of a forest (a list of trees). Symbolically:
f: <nowiki>[t[1], ..., t[k]]</nowiki>
t: v f
A forest ''f'' consists of a list of trees, while a tree ''t'' consists of a pair of a value ''v'' and a forest ''f'' (its children). This definition is elegant and easy to work with abstractly (such as when proving theorems about properties of trees), as it expresses a tree in simple terms: a list of one type, and a pair of two types. Further, it matches many algorithms on trees, which consist of doing one thing with the value, and another thing with the children.


This mutually recursive definition can be converted to a singly recursive definition by inlining the definition of a forest:
そのパワーのライブこの雷起源。 '周フオはそれを信じることができなかった。<br><br>しかし、6盛黄とどのように秦ゆうqingpao知ることは、実際にそれ自体が第一級の無名Lingbaoです [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_14.php クリスチャンルブタン メンズ 靴]。今まで霊の世界から生まれた、第一級の無名Lingbao壊れた状況がなかった [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_3.php クリスチャンルブタン 店舗]。沸騰文学101​​du [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_10.php クリスチャンルブタン ブーツ].netへようこそ<br><br>結局のところ、あまりにもハードまでファーストクラスの無名Lingbao。<br><br>qingpao秦Yuの身体の防御、自然に、非常に恐ろしい。そして......攻撃者はqingpao秦ゆうの断片化を行うことができたとしても、qingpao秦ゆう孟は冷ガスに変換することができます。<br><br>不滅!<br><br>は '周フオは、これはあなたの土地の力とは?紳士盛黄、あなたはハワードは私に対して一週間に秦ゆうに頼ることができると思いますか?'黒いローブは遠くないダン·シャオ秦ゆうから立っていたと述べたが、現時点では、黒のローブ秦ゆう片手で、それは神秘的な黄色の斑点がある古風な黒小銃、ライフル銃です。<br><br>雪の殺し屋、Lingbaoは、よりファーストクラスあいまいテロ雪の殺し屋より、秦Yuは最終的に出てきた [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_10.php クリスチャンルブタン 銀座]<br>攻撃 [http://www.lamartcorp.com/modules/mod_menu/rakuten_cl_4.php クリスチャンルブタン パンプス]?雪の神より<br>
t: v <nowiki>[t[1], ..., t[k]]</nowiki>
相关的主题文章:
A tree ''t'' consists of a pair of a value ''v'' and a list of trees (its children). This definition is more compact, but somewhat messier: a tree consists of a pair of one type and a list of another, which require disentangling to prove results about.
<ul>
 
 
In [[Standard ML]], the tree and forest data types can be mutually recursively defined as follows, allowing empty trees:{{sfn|Harper|2000|loc="[http://www.cs.cmu.edu/~rwh/introsml/core/datatypes.htm Date Types]"}}
  <li>[http://www2t.biglobe.ne.jp/ugo2/bbs/yybbs.cgi http://www2t.biglobe.ne.jp/ugo2/bbs/yybbs.cgi]</li>
<source lang=ocaml>
 
datatype 'a tree = Empty | Node of 'a * 'a forest
  <li>[http://www.british1.co.uk/cgi-bin/search.cgi http://www.british1.co.uk/cgi-bin/search.cgi]</li>
and      'a forest = Nil | Cons of 'a tree * 'a forest
 
</source>
  <li>[http://pattson.com/cgi-bin/guestbook/guestbook.cgi http://pattson.com/cgi-bin/guestbook/guestbook.cgi]</li>
 
 
===Computer functions===
</ul>
Just as algorithms on recursive data types can naturally be given by recursive functions, algorithms on mutually recursive data structures can be naturally given by mutually recursive functions. Common examples include algorithms on trees, and [[recursive descent parser]]s. As with direct recursion, [[tail call optimization]] is necessary if the recursion depth is large or unbounded, such as using mutual recursion for multitasking. Note that tail call optimization in general (when the function called is not the same as the original function, as in tail-recursive calls) may be more difficult to implement than the special case of tail-recursive call optimization, and thus efficient implementation of mutual tail recursion may be absent from languages that only optimize tail-recursive calls. In languages such as [[Pascal (programming language)|Pascal]] that require declaration before use, mutually recursive functions require [[forward declaration]], as a forward reference cannot be avoided when defining them.
 
As with directly recursive functions, a [[Recursion (computer science)#Wrapper function|wrapper function]] may be useful, with the mutually recursive functions [[nested function]]s within its scope if this is supported. This is particularly useful for sharing state across a set of functions without having to pass parameters between them.
 
====Basic examples====
A standard example of mutual recursion, which is admittedly artificial, is determining whether a non-negative number is even or is odd by having two separate functions and calling each other, decrementing each time.{{sfn|Hutton|2007|loc=6.5 Mutual recursion, pp. [http://books.google.com/books?id=olp7lAtpRX0C&pg=PA53&dq=%22mutual+recursion%22 53–55]}} In C:
<source lang=C>
bool is_even(unsigned int n)
    if (n == 0)
        return true;
    else
        return is_odd(n - 1);
 
bool is_odd(unsigned int n)
    if (n == 0)
        return false;
    else
        return is_even(n - 1);
</source>
 
These functions are based on the observation that the question ''is 4 even?'' is equivalent to ''is 3 odd?'', which is in turn equivalent to ''is 2 even?'', and so on down to 0. This example is mutual [[single recursion]], and could easily be replaced by iteration. In this example, the mutually recursive calls are [[tail call]]s, and tail call optimization would be necessary for this to execute in constant stack space; in C this would take ''O''(''n'') stack space, unless rewritten to use jumps instead of calls.
<ref>"[http://www.cs.bu.edu/~hwxi/ATS/DOCUMENT/TUTORIALATS/HTML/c244.html Mutual Tail-Recursion]" and "[http://www.cs.bu.edu/~hwxi/ATS/TUTORIAL/contents/tail-recursive-functions.html Tail-Recursive Functions]", ''[http://www.cs.bu.edu/~hwxi/ATS/DOCUMENT/TUTORIALATS/HTML/book1.html A Tutorial on Programming Features in ATS],'' Hongwei Xi, 2010</ref> This could be reduced to a single recursive function <code>is_even</code>, with <code>is_odd</code> calling <code>is_even</code>, but <code>is_even</code> only calling itself, with <code>is_odd</code> inlined.
 
As a more general class of examples, an algorithm on a tree can be decomposed into its behavior on a value and its behavior on children, and can be split up into two mutually recursive functions, one specifying the behavior on a tree, calling the forest function for the forest of children, and one specifying the behavior on a forest, calling the tree function for the tree in the forest.  In Python:
<source lang=python>
def f_tree(tree):
    f_value(tree.value)
    f_forest(tree.children)
 
def f_forest(forest):
    for tree in forest:
        f_tree(tree)
</source>
In this case the tree function calls the forest function by single recursion, but the forest function calls the tree function by [[multiple recursion]].
 
Using the Standard ML data type above, the size of a tree (number of nodes) can be computed via the following mutually recursive functions:{{sfn|Harper|2000|loc="[http://www.cs.cmu.edu/~rwh/introsml/core/datatypes.htm Date Types]"}}
<source lang=ocaml>
fun size_tree Empty = 0
  | size_tree (Node (_, f)) = 1 + size_forest f
and size_forest Nil = 0
  | size_forest (Cons (t, f')) = size_tree t + size_forest f'
</source>
 
A more detailed example in Scheme, counting the leaves of a tree:{{sfn|Harvey|Wright|1999|loc=V. Abstraction: 18. Trees: Mutual Recursion, pp. [http://books.google.co.jp/books?id=igJRhp0KGn8C&pg=PA310&dq=%22mutual%20recursion%22 310–313]}}
<source lang=scheme>
(define (count-leaves tree)
  (if (leaf? tree)
      1
      (count-leaves-in-forest (children tree))))
 
(define (count-leaves-in-forest forest)
  (if (null? forest)
      0
      (+ (count-leaves (car forest))
        (count-leaves-in-forest (cdr forest)))))
</source>
 
These examples reduce easily to a single recursive function by inlining the forest function in the tree function, which is commonly done in practice: directly recursive functions that operate on trees sequentially process the value of the node and recurse on the children within one function, rather than dividing these into two separate functions.
 
====Advanced examples====
A more complicated example is given by [[recursive descent parser]]s, which can be naturally implemented by having one function for each [[Production (computer science)|production rule]] of a grammar, which then mutually recurse; this will in general be multiple recursion, as production rules generally combine multiple parts. This can also be done without mutual recursion, for example by still having separate functions for each production rule, but having them called by a single controller function, or by putting all the grammar in a single function.
 
Mutual recursion can also implement a [[finite-state machine]], with one function for each state, and single recursion in changing state; this requires tail call optimization if the number of state changes is large or unbounded. This can be used as a simple form of [[cooperative multitasking]]. A similar approach to multitasking is to instead use [[coroutine]]s which call each other, where rather than terminating by calling another routine, one coroutine yields to another but does not terminate, and then resumes execution when it is yielded back to. This allows individual coroutines to hold state, without it needing to be passed by parameters or stored in shared variables.
 
There are also some algorithms which naturally have two phases, such as [[minimax]] (min and max), and these can be implemented by having each phase in a separate function with mutual recursion, though they can also be combined into a single function with direct recursion.
 
===Mathematical functions===
In mathematics, the [[Hofstadter Female and Male sequences]] are an example of a pair of integer sequences defined in a mutually recursive manner.
 
Fractals can be computed (up to a given resolution) by recursive functions. This can sometimes be done more elegantly via mutually recursive functions; the [[Sierpiński curve]] is a good example.
 
==Prevalence==
Mutual recursion is very common in the [[functional programming]] style, and is often used for programs written in [[Lisp programming language|LISP]], [[Scheme (programming language)|Scheme]], [[ML programming language|ML]], and similar [[programming language|languages]]. In languages such as [[Prolog programming language|Prolog]], mutual recursion is almost unavoidable.
 
Some programming styles discourage mutual recursion, claiming that it can be confusing to distinguish the conditions which will return an answer from the conditions that would allow the code to run forever without producing an answer. [[Peter Norvig]] points to a [[design pattern]] which discourages the use entirely, stating:<ref>[http://norvig.com/sudoku.html Solving Every Sudoku Puzzle]</ref>
{{quote|text=If you have two mutually-recursive functions that both alter the state of an object, try to move almost all the functionality into just one of the functions. Otherwise you will probably end up duplicating code.}}
 
==Terminology==
Mutual recursion is also known as [[indirect recursion]], by contrast with [[direct recursion]], where a single function calls itself directly. This is simply a difference of emphasis, not a different notion: "indirect recursion" emphasises an individual function, while "mutual recursion" emphasises the set of functions, and does not single out an individual function. For example, if ''f'' calls itself, that is direct recursion. If instead ''f'' calls ''g'' and then ''g'' calls ''f,'' which in turn calls ''g'' again, from the point of view of ''f'' alone, ''f'' is indirectly recursing, while from the point of view of ''g'' alone, ''g'' is indirectly recursing, while from the point of view of both, ''f'' and ''g'' are mutually recursing on each other. Similarly a set of three or more functions that call each other can be called a set of mutually recursive functions.
 
== Conversion to direct recursion ==
 
Mathematically, a set of mutually recursive functions are [[primitive recursive]], which can be proven by [[course-of-values recursion]],<ref>"[http://planetmath.org/mutualrecursion mutual recursion]", ''PlanetMath''</ref> building a single function ''F'' that lists the values of the individual recursive function in order: <math>F = f_1(0), f_2(0), f_1(1), f_2(1), \dots,</math> and rewriting the mutual recursion as a primitive recursion.
 
Any mutual recursion between two procedures can be converted to direct recursion by inlining the code of one procedure into the other.<ref>[http://delivery.acm.org/10.1145/180000/176510/p151-kaser.pdf?key1=176510&key2=1857140721&coll=GUIDE&dl=GUIDE&CFID=82873082&CFTOKEN=54657523 On the Conversion of Indirect to Direct Recursion] by Owen Kaser, C. R. Ramakrishnan, and Shaunak Pawagi at [[State University of New York, Stony Brook]] (1993)</ref> If there is only one site where one procedure calls the other, this is straightforward, though if there are several it can involve code duplication. In terms of the call stack, two mutually recursive procedures yield a stack ABABAB..., and inlining B into A yields the direct recursion (AB)(AB)(AB)...
 
Alternately, any number of procedures can be merged into a single procedure that takes as argument a [[variant record]] (or [[algebraic data type]]) representing the selection of a procedure and its arguments; the merged procedure then dispatches on its argument to execute the corresponding code and uses direct recursion to call self as appropriate.  This can be seen as a limited application of [[defunctionalization]].<ref>{{cite conference
  | first = John | last = Reynolds | authorlink = John_C._Reynolds
  | title = Definitional Interpreters for Higher-Order Programming Languages
  | booktitle = Proceedings of the ACM Annual Conference
  | date = August 1972
  | location = Boston, Massachusetts
  | pages = 717–740
  | url = http://www.brics.dk/~hosc/local/HOSC-11-4-pp363-397.pdf
  }}</ref>  This translation may be useful when any of the mutually recursive procedures can be called by outside code, so there is no obvious case for inlining one procedure into the other. Such code then needs to be modified so that procedure calls are performed by bundling arguments into a variant record as described; alternately, wrapper procedures may be used for this task.
 
== See also ==
 
* [[Recursion (computer science)]]
* [[Circular dependency]]
 
== References ==
{{reflist|2}}
* {{citation|first=Robert|last=Harper|authorlink=Robert Harper (computer scientist)|url=http://www.cs.cmu.edu/~rwh/introsml/|title=Programming in Standard ML|year=2000}}
* {{cite isbn|9780262082815}}
* {{cite isbn|9780521692694}}
 
== External links ==
* [http://rosettacode.org/wiki/Mutual_recursion Mutual recursion] at [[Rosetta Code]]
* "[http://stackoverflow.com/questions/10295735/example-demonstrating-good-use-of-mutual-recursion Example demonstrating good use of mutual recursion]", "[http://stackoverflow.com/questions/2725038/are-there-any-example-of-mutual-recursion Are there any example of Mutual recursion?]", ''Stack Overflow''
 
[[Category:Theory of computation]]
[[Category:Recursion]]

Latest revision as of 23:25, 14 December 2014

Chixuedongfu北築山、秦ゆう正ボレースタンド

があり、スピーカー、彼は緑のヒョンチョンチョンは彼に近いLaoguiとチャールズハング強度が気にしないだろうと述べていることを聞いたからです。
現時点秦ゆうで
、侯飛、三兄弟心に黒い羽は、一般的な文字は、彼らに脅威がなく、今では最大の敵は、残りの9 クリスチャンルブタン通販 8悪悪紫龍の寺院です。 「ブラック、あなたは退却を行く元英を洗練開始しました。その後、いくつかの私たちの生活の中での時間とよりエキサイティングが起こるのだろう、低強度は、そのフィールドのイベントに参加する資格がないかもしれません。 '秦ゆう口笑顔で、目Limang光った。秦三兄弟、両方の将軍秦。今日の皇帝秦鄭は、時間までの冷酷なわずかな慈悲がないでしょう。秦Yuは彼が今考慮し、最悪の場合、秦ゆう、何を恐れて考えられ、最悪の場合を取り、する必要のあるタイムに関係なく、また、将来の期待、の場合ではありません。ブラックフェザー。弱いなしの侯飛、あらゆる混練柿。 クリスチャンルブタン通販 「私たちは自由に三兄弟をこねるしたい、私たちは彼の手が切断されたことを注意しなければならない。 クリスチャンルブタン メンズ 靴 '秦Yuは外部に見える。

クリスチャンルブタン メンズ 通販......

Chixuedongfu北築山、秦ゆう正ボレースタンド 靴 クリスチャンルブタン。 相关的主题文章:

は '周フオは、これはあなたの土地の力とは

そのパワーのライブこの雷起源。 '周フオはそれを信じることができなかった。

しかし、6盛黄とどのように秦ゆうqingpao知ることは、実際にそれ自体が第一級の無名Lingbaoです クリスチャンルブタン メンズ 靴。今まで霊の世界から生まれた、第一級の無名Lingbao壊れた状況がなかった クリスチャンルブタン 店舗。沸騰文学101​​du クリスチャンルブタン ブーツ.netへようこそ

結局のところ、あまりにもハードまでファーストクラスの無名Lingbao。

qingpao秦Yuの身体の防御、自然に、非常に恐ろしい。そして......攻撃者はqingpao秦ゆうの断片化を行うことができたとしても、qingpao秦ゆう孟は冷ガスに変換することができます。

不滅!

は '周フオは、これはあなたの土地の力とは?紳士盛黄、あなたはハワードは私に対して一週間に秦ゆうに頼ることができると思いますか?'黒いローブは遠くないダン·シャオ秦ゆうから立っていたと述べたが、現時点では、黒のローブ秦ゆう片手で、それは神秘的な黄色の斑点がある古風な黒小銃、ライフル銃です。

雪の殺し屋、Lingbaoは、よりファーストクラスあいまいテロ雪の殺し屋より、秦Yuは最終的に出てきた クリスチャンルブタン 銀座
攻撃 クリスチャンルブタン パンプス?雪の神より
」 相关的主题文章: