L4K ︰ Python Turtle《十中》

小海龜沉吟『無名天地之始,有名萬物之母』耶!!『體、用祇是選擇』的乎??

又為什麼『』與『』的 λ表達式是

TRUE =_{df} (\lambda  x. ( \lambda y. x))
FALSE =_{df} (\lambda  x .( \lambda y. y))

的呢?如果我們將『運算』看成『黑箱』,用『實驗』的方法來『研究』輸入輸出的『關係』,這一組有兩個輸入端的黑箱,對於任意的輸入『二元組』pair  (u, v),有︰

(((\lambda x. ( \lambda y. x)) u) v) = u
(((\lambda x. ( \lambda y. y)) u) v) = v

,於是將結論歸結成︰貼『』標籤的箱子的『作用』是『選擇』輸入的『第一項』將之輸出;而貼『』標籤的箱子的『作用』是『選擇』輸入的『第二項』將之輸出。

假使一位『軟體』工程師在函式『除錯』時,可能會採取在那個『函式』內『輸出』看看得到的『輸入』參數值是否正確?

於是將結論歸結成︰』標識符的函式『作用』是『選擇』輸入參數的『第一項』;『』標識符的函式『作用』是『選擇』輸入參數的『第二項』。

那麼對一個已經打開的『白箱』,又知道作用的『函式』,怎麼會概念上『一頭霧水』的呢??如果細思一個邱奇自然數『 0 』, 0 =_{df} (\lambda f. ( \lambda x. x)),這跟『』的 λ表達式有什麼不一樣的呢?那難道我們能說『0』就是『』的嗎?在《布林代數》中的『0』與『1』其實是未定義的『兩態』基元概念── 就像歐式幾何學裡的『』、『』和『』是『基本』概念一樣 ──,因此不管說它是『電壓高低』或者講它是『電流有無』的『數位設計』可以應用布林代數。要是我們將『0』『1』與『』『』概念連繫起來看,『布林邏輯』就是『真假』是什麼的『系統化』之概念內涵開展,它的『整體內容』呈現『兩態邏輯』的『方方面面』,縱使至於『孤虛』NAND 一個邏輯概念就足夠了,對於『 0 與 1 』概念本身還是『三緘其口』。

其實『 λ表達式』建構上表徵『函式』── 假使代表某種『概念』──的『子函式組織』以及『子函式應用』之『結構』。而這個所謂的『結構』只是在『 λ語言』的『語法』上『呈現』的,所以想要如何『語意詮釋』呢?也許天下《一指》可知,萬物《一馬》可比的吧!!

── 摘自《λ 運算︰概念導引之《補充》※真假祇是個選擇??

 

義理分辨雖清晰︰

Logical analysis of the recursive solution

As in many mathematical puzzles, finding a solution is made easier by solving a slightly more general problem: how to move a tower of h (h=height) disks from a starting peg A (f=from) onto a destination peg C (t=to), B being the remaining third peg and assuming tf. First, observe that the problem is symmetric for permutations of the names of the pegs (symmetric group S3). If a solution is known moving from peg A to peg C, then, by renaming the pegs, the same solution can be used for every other choice of starting and destination peg. If there is only one disk (or even none at all), the problem is trivial. If h=1, then simply move the disk from peg A to peg C. If h>1, then somewhere along the sequence of moves, the largest disk must be moved from peg A to another peg, preferably to peg C. The only situation that allows this move is when all smaller h−1 disks are on peg B. Hence, first all h−1 smaller disks must go from A to B. Then move the largest disk and finally move the h−1 smaller disks from peg B to peg C. The presence of the largest disk does not impede any move of the h−1 smaller disks and can temporarily be ignored. Now the problem is reduced to moving h−1 disks from one peg to another one, first from A to B and subsequently from B to C, but the same method can be used both times by renaming the pegs. The same strategy can be used to reduce the h−1 problem to h−2, h−3, and so on until only one disk is left. This is called recursion. This algorithm can be schematized as follows. Identify the disks in order of increasing size by the natural numbers from 0 up to but not including h. Hence disk 0 is the smallest one and disk h−1 the largest one.

The following is a procedure for moving a tower of h disks from a peg A onto a peg C, with B being the remaining third peg:

  • Step 1: If h>1 then first use this procedure to move the h−1 smaller disks from peg A to peg B.
  • Step 2: Now the largest disk, i.e. disk h can be moved from peg A to peg C.
  • Step 3: If h>1 then again use this procedure to move the h−1 smaller disks from peg B to peg C.

By means of mathematical induction, it is easily proven that the above procedure requires the minimal number of moves possible, and that the produced solution is the only one with this minimal number of moves. Using recurrence relations, the exact number of moves that this solution requires can be calculated by:  2^h - 1. This result is obtained by noting that steps 1 and 3 take  T_{h-1} moves, and step 2 takes one move, giving  T_h = 2T_{h-1} + 1.

 

虛名實位怎解惑︰

pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 成 = ['極', '大', '中', '小']
>>> 住 = []
>>> 壞 = []
>>> 
>>> def 河內塔(層, 始, 終, 中):
...     if 層 > 0:
...         河內塔(層 - 1, 始, 中, 終)
...         終.append(始.pop())
...         print(成, 住, 壞, '============', sep='\n')
...         河內塔(層 - 1, 中, 終, 始)
... 
>>> 
>>> 河內塔(4, 成, 壞, 住) 
['極', '大', '中']
['小']
[]
============
['極', '大']
['小']
['中']
============
['極', '大']
[]
['中', '小']
============
['極']
['大']
['中', '小']
============
['極', '小']
['大']
['中']
============
['極', '小']
['大', '中']
[]
============
['極']
['大', '中', '小']
[]
============
[]
['大', '中', '小']
['極']
============
[]
['大', '中']
['極', '小']
============
['中']
['大']
['極', '小']
============
['中', '小']
['大']
['極']
============
['中', '小']
[]
['極', '大']
============
['中']
['小']
['極', '大']
============
[]
['小']
['極', '大', '中']
============
[]
[]
['極', '大', '中', '小']
============
>>> 

 

坐忘觀想千百遍︰

Recursive solution

The key to solving a problem recursively is to recognize that it can be broken down into a collection of smaller sub-problems to each of which that same general solving procedure that we are seeking applies, and the total solution is then found in some simple way from those sub-problems’ solutions. Each of thus created sub-problems being “smaller” guarantees the base case(s) will eventually be reached. Thence, for the Towers of Hanoi:

  • label the pegs A, B, C
  • let n be the total number of discs
  • number the discs from 1 (smallest, topmost) to n (largest, bottom-most)

Assuming all n disks are distributed in valid arrangements among the pegs; assuming there are m top disks on a source peg, and all the rest of the disks are larger than m so can be safely ignored; to move m discs from a source peg to a target peg using a spare peg, without violating the rules:

  1. move m−1 discs from the source to the spare peg, by the same general solving procedure. Rules are not violated, by assumption. This leaves the disk m as a top disk on the source peg
  2. move the disc m from the source to the target peg, which is guaranteed to be a valid move, by the assumptions — a simple step
  3. move the m−1 discs that we’ve just placed on the spare, from the spare to the target peg by the same general solving procedure, so they are placed on top of the disc m without violating the rules
  4. the base case being, to move 0 disks, do nothing.

The full Tower of Hanoi solution then consists of moving n disks from the source peg A to the target peg C, using B as the spare peg.

The above is a recursive algorithm. To carry out steps 1 and 3 apply the same algorithm again for m−1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for m = 0, which means there’s nothing to do. This approach can be given a rigorous mathematical formalism with the theory of dynamic programming,[7][8] and is often used as an example of recursion when teaching programming.

Illustration of a recursive solution for the Towers of Hanoi puzzle with 4 disks.

 

誰敢操演累死哦☆★