Skip to Content
深入理解運算原理|從簡單的機器到無所不能的程式
book

深入理解運算原理|從簡單的機器到無所不能的程式

by Tom Stuart
November 2017
Beginner to intermediate
344 pages
7h 2m
Chinese
GoTop Information, Inc.
Content preview from 深入理解運算原理|從簡單的機器到無所不能的程式
186
|
6
當我們以
m
n
的值呼叫
MOD
,而且 Ruby 開始估算 proc 裡的主體時,它會到達
MOD[SUBTRACT[m][n]][n]
的遞迴呼叫,並在引數傳給
IF
的時候,隨即開始估算它,而不
論將
IS_LESS_OR_EQUAL[n][m]
評估成
TRUE
還是
FALSE
。第 2 次呼叫
MOD
會導致另一次無
條件的遞迴呼叫等等,因此就變成無限遞迴。
為了解決這個問題,我們需要有個方式讓 Ruby 知道:直到我們確定需要
IF
的第 2
引數之前,Ruby 都先不要估算
IF
的第 2 個引數(也就是延後估算)。Ruby 裡的任何運
算式皆可因包進 proc 而獲得延後估算,但將任意的 Ruby 值包進 proc,通常會改變它
的含義(例如,
1 + 2
的結果不等於
-> {1 + 2}
,所以我們可能需要更聰明一點。
所幸我們不需要,因為這是特殊狀況:我們知道呼叫
MOD
的結果將會是個單一引數的
proc,因為我們所有的值都是單一引數的 procs,而且我們已經(從第 170 頁的『相
等』)得知,以另一個 proc 需要相同引數作為
p
並以它們立即呼叫
p
來包裝任何 proc
p
,將會產生無法與
p
區別的值,所以我們可以使用這樣的技巧來延後遞迴呼叫,也不
會影響將值傳進
IF
的含義:
MOD =
-> m { -> n {
IF[IS_LESS_OR_EQUAL[n][m]][
-> x {
MOD[SUBTRACT[m][n]][n][x]
}
][
m
]
} }
這會將遞迴的
MOD
呼叫包進 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JSON實務手冊

JSON實務手冊

Tom Marrs

Publisher Resources

ISBN: 9789864766000