iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
0
Software Development

mostly:functional 從零開始的異世界程式觀 --- 函數式程式設計的試煉系列 第 20

mostly:functional 第十九章:Semigroup 的法則

Something is elegant if it is two things at once: unusually simple and surprisingly powerful.
兼備異常簡潔、意外強大者,可謂之優雅。

-- Matthew E. May, In Pursuit of Elegance


-- 0821
上次從標示著 class 的建築出來後,外面的天色明顯比之前亮很多,不再像是沒什麼光線的夜晚,而是像清晨太陽正要昇起的感覺。

為什麼要那麼刻意的引導我去那棟建築呢?我想了很久。

-- 0825

我忽然有種感覺,會不會是因為…Monad 是…一種 typeclass?帶著這個想法,我往牆那邊走。但離牆還有一段距離地方,我看到有一群之前還只有輪廓的建築,現在看起來都變得比較…具體一點了。其中有一棟最為明顯,門楣上面的標示是… <>。啊,這個我有印象。

再靠近建築一些,就發現那塊金屬板就飄浮在門的前方。那麼下一站應該就是這裡了,我看到金屬板上,寫著這些文字:

Semigroup 的實作:

class Semigroup a where
 (<>) :: a -> a -> a
 {-# MINIMAL (<>) #-}

Semigroup 的法則:

  1. 封閉律
  2. 結合律

第一段看起來像是要實作一個叫 Semigorup 的 typeclass,需要做出 <> 這個函式,它的型別是 a -> a -> a,所以是接受兩個值,回傳一個值的函式。

第二段的法則…封閉律跟結合律…好像在哪裡聽過的樣子…




我們要先回到……數學。很簡單的,國小程度的數學。我們先來再來思考一次整數,以及加法。首先我們要知道加法是一種二元運算:把兩個整數相加,會得到一個結果。

整數的加法符合以下幾條規則:

  1. 封閉律
    任何兩個整數相加,其結果也是整數

  2. 結合律
    三個整數相加時,先相加左邊兩個,最後再加上右邊的,其結果等同於先相加右邊兩個,最後再加上左邊的。

    https://chart.googleapis.com/chart?cht=tx&amp;chl=(1%20%2B%202)%20%2B%203 其結果等同於 https://chart.googleapis.com/chart?cht=tx&amp;chl=1%20%2B%20(2%20%2B%203)

https://chart.googleapis.com/chart?cht=tx&amp;chl=(1%20%2B%202)%20%2B%203%20%3D%201%20%2B%20(2%20%2B%203)


抽象

接下來我們要把上面的說法抽象一層。也就是"整數",跟"加法運算"都抽象出來。先想像一下,有一堆同類的東西,然後有一種作用在這類東西上的二元運算。為了比較好理解,我們把這類的東西稱為 https://chart.googleapis.com/chart?cht=tx&amp;chl=S,並且把這個二元運算用 https://chart.googleapis.com/chart?cht=tx&amp;chl=%3C%3E 表示。接著從 https://chart.googleapis.com/chart?cht=tx&amp;chl=S 裡隨便挑幾個成員,假設我們挑到 https://chart.googleapis.com/chart?cht=tx&amp;chl=a%2C%20b%2C%20c 這幾個。

在想像的過程中,你可以試著對應整數的例子, https://chart.googleapis.com/chart?cht=tx&amp;chl=S 就是所有的整數, https://chart.googleapis.com/chart?cht=tx&amp;chl=a%2C%20b%2C%20c 就是 https://chart.googleapis.com/chart?cht=tx&amp;chl=1%2C%202%2C%203 這種真正的數字。而二元運算 https://chart.googleapis.com/chart?cht=tx&amp;chl=%3C%3E 就是 https://chart.googleapis.com/chart?cht=tx&amp;chl=%2B

那麼這類東西要符合這些規則:

  1. 封閉律
    任兩個 https://chart.googleapis.com/chart?cht=tx&amp;chl=S 的成員,例如 https://chart.googleapis.com/chart?cht=tx&amp;chl=ahttps://chart.googleapis.com/chart?cht=tx&amp;chl=b,那麼 https://chart.googleapis.com/chart?cht=tx&amp;chl=a%20%3C%3E%20b 的結果也會是 https://chart.googleapis.com/chart?cht=tx&amp;chl=S 的成員。

  2. 結合律
    任三個 https://chart.googleapis.com/chart?cht=tx&amp;chl=S 的成員,例如https://chart.googleapis.com/chart?cht=tx&amp;chl=ahttps://chart.googleapis.com/chart?cht=tx&amp;chl=bhttps://chart.googleapis.com/chart?cht=tx&amp;chl=c,要把他們 https://chart.googleapis.com/chart?cht=tx&amp;chl=%3C%3E 在一起時,先算哪兩個沒有差別。也就是:

https://chart.googleapis.com/chart?cht=tx&amp;chl=(a%20%3C%3E%20b)%20%3C%3E%20c%20%3D%20a%20%3C%3E%20(b%20%3C%3E%20c)

符合這兩條法則的,我們稱之為 Semigroup (半群)




所以這麼說來,整數就是 Semigroup 的一種囉?我試著在附近找到一些整數,丟進門上的缺口裡。

沒有反應。

為什麼?

[to be continue]


上一篇
mostly:functional 第十八章:不同事物的相同部份
下一篇
mostly:functional 第二十章:Semigroup 的實體
系列文
mostly:functional 從零開始的異世界程式觀 --- 函數式程式設計的試煉35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言