功能程式設計 – 在函數語言程式設計中是否還有任何代數結構,而不是單一的?
我最近知道函數語言程式設計(在Haskell和Scala中).它的功能和優雅是相當迷人.
但是當我遇到一個名叫Monoid的代數結構的Monads時,我很驚訝,很高興看到我從數學中學到的理論知識在程式設計中得到了應用.
這個觀察在我的腦海中引起了一個問題:組織,領域或環(參見ofollow,noindex" target="_blank">Algebraic Structures 為其他人)可以在程式設計中用於更多的抽象和程式碼重用目的,並實現數學相似的程式設計?
我知道,這個名為Fortress 的語言(當編譯器完成時,我一定會喜歡任何語言一次)在庫程式碼中定義了這些結構.但是目前為止,我看到的僅僅是我們已經熟悉的數字型別.他們會有其他用途嗎?
最好的祝福,
ciun
您可以建模許多結構.這是一個組:
class Group a where mult :: a -> a -> a identity :: a inverse :: a -> a instance Group Integer where mult = (+) identity = 0 inverse = negate -- S_3 (group of all bijections of a 3-element set) data S3 = ABC | ACB | BAC | BCA | CAB | CBA instance Group S3 where mult ABC x = x ... -- some boring code identity = ABC inverse ABC = ABC ... -- remaining cases -- Operations on groups. Dual: data Dual a = Dual { getDual :: a } instance Group a => Group (Dual a) where mult (Dual x) (Dual y) = Dual (mult y x) identity = Dual identity inverse (Dual x) = Dual (inverse x) -- Product: instance (Group a, Group b) => Group (a,b) where mult (x,y) (z,t) = (x `mult` z, y `mult` t) identity = (identity, identity) inverse (x,y) = (inverse x, inverse y)
現在,您可以編寫多(雙CAB,5)(雙CBA,1)並獲得結果.這將是組S3 *⨯Z中的計算.您可以新增其他組,以任何可能的方式組合它們,並與它們進行計算.
類似的事情可以通過環,欄位,排序,向量空間,類別等來完成.Haskell的數字層次結構不幸的是被模仿,但是有一個numeric prelude 嘗試修復它.還有DoCon 把它帶到極限.對於型別課程(主要由類別理論推動),有Typeclassopedia 具有大量示例和應用程式.
http://stackoverflow.com/questions/3331287/is-there-any-algebraic-structures-used-in-functional-programming-other-then-mono