雖然 TurtleArt
能寫作美麗之『遞迴』程式︰
Recursion
Recursion occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition. While this apparently defines an infinite number of instances (function values), it is often done in such a way that no loop or infinite chain of references can occur.
Formal definitions
In mathematics and computer science, a class of objects or methods exhibit recursive behavior when they can be defined by two properties:
- A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer
- A set of rules that reduce all other cases toward the base case
For example, the following is a recursive definition of a person’s ancestors:
- One’s parents are one’s ancestors (base case).
- The ancestors of one’s ancestors are also one’s ancestors (recursion step).
The Fibonacci sequence is a classic example of recursion:
Many mathematical axioms are based upon recursive rules. For example, the formal definition of the natural numbers by the Peano axioms can be described as: 0 is a natural number, and each natural number has a successor, which is also a natural number. By this base case and recursive rule, one can generate the set of all natural numbers.
Recursively defined mathematical objects include functions, sets, and especially fractals.
There are various more tongue-in-cheek “definitions” of recursion; see recursive humor.
……
The recursion theorem
In set theory, this is a theorem guaranteeing that recursively defined functions exist. Given a set X, an element a of X and a function , the theorem states that there is a unique (where denotes the set of natural numbers including zero) such that
for any natural number n.
Proof of uniqueness
Take two functions and such that:
where a is an element of X.
It can be proved by mathematical induction that
for all natural numbers n:
- Base Case: so the equality holds for .
- Inductive Step: Suppose for some . Then
-
- Hence F(k) = G(k) implies F(k+1) = G(k+1).
By induction, for all .
───
終究像用『 λ表達式』之『Y組合子』表現『遞迴』般的咬文嚼字 ?故而特此介紹『派生三』 Python3 之『小海龜繪圖』模組︰
24.1. turtle
— Turtle graphics
Source code: Lib/turtle.py
24.1.1. Introduction
Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle
, give it the command turtle.forward(15)
, and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25)
, and it rotates in-place 25 degrees clockwise.
The turtle
module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5.
It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n
switch.
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter
for the underlying graphics, it needs a version of Python installed with Tk support.
The object-oriented interface uses essentially two+two classes:
-
The
TurtleScreen
class defines graphics windows as a playground for the drawing turtles. Its constructor needs atkinter.Canvas
or aScrolledCanvas
as argument. It should be used whenturtle
is used as part of some application.The function
Screen()
returns a singleton object of aTurtleScreen
subclass. This function should be used whenturtle
is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
-
RawTurtle
(alias:RawPen
) defines Turtle objects which draw on aTurtleScreen
. Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw.Derived from RawTurtle is the subclass
Turtle
(alias:Pen
), which draws on “the”Screen
instance which is automatically created, if not already present.All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.
The procedural interface provides functions which are derived from the methods of the classes Screen
and Turtle
. They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.
To use multiple turtles on a screen one has to use the object-oriented interface.
Note
In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here.
盼將『遞迴』之奧妙『視覺化』也!!