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

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

by Tom Stuart
November 2017
Beginner to intermediate
344 pages
7h 2m
Chinese
GoTop Information, Inc.
Content preview from 深入理解運算原理|從簡單的機器到無所不能的程式
無為的程式設計
|
181
數值運算
現在我們有數值、布林值、條件式、述語、對組,幾乎已經準備好重新實作模數運算子
了。
但在開始之前,我們需要能執行更簡單的運算,諸如遞增和遞減單一數值。遞增相當直
覺易懂:
INCREMENT = -> n { -> p { -> x { p[n[p][x]] } } }
讓我們來看看
INCREMENT
的運作方式:我們使用以 proc 為基礎的數值
n
來呼叫它,它會
傳回新的 proc,這個新的 proc 需要一些其他的 proc
p
和一些任意的第 2 個引數
x
,就
如同數值一般。
當我們呼叫這個新的 proc 的時候,它會做什麼?首先,它會以
p
x
呼叫
n
(因為
n
數值,也就是『在 x 呼叫
p
n
次』,就像原始數值所完成的),然後在此結果再次呼叫
p
一次。大致而言,會在這個 proc 的第 2 個引數呼叫第 1 個引數
n + 1
次,也就是如何
正確表示數值
n + 1
但是遞減要怎麼做呢?這看似是更難的問題:只要已經呼叫
n
proc,就很容易新增
額外的呼叫,因此這雖然會呼叫
n + 1
次,但並沒有明顯的作法可『還原』它們其中一
個,而變成
n - 1
次呼叫。
其中一個解決方案是設計 proc,在某些初始引數呼叫了
n
次時,傳回數值
n - 1
。所幸
對組提供了完成這項工作的正確方式。想想這個 Ruby 方法做了什麼:
def slide(pair)
[pair.last, pair.last + 1]
end
當我們以雙元素陣列的數值呼叫
slide
時,它會傳回新的雙元素陣列,元素裡包含第 ...
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