我知道 Functor 型別類是這樣定義的:
class Functor f where
fmap :: (a -> b) -> f a -> f b
在我的課程中,它說這是將串列作為仿函式實體的方式:
instance Functor [] where
fmap = map
是[] a某種同義詞[a]嗎?
uj5u.com熱心網友回復:
是
[] a某種同義詞[a]
是的,確切地說。這兩種型別沒有區別:后者只是前者的一個漂亮的符號。
在 Haskell 中,所有型別都遵循T arg1 arg2 ...where Tis some type constructor 的語法,但其中一些還具有在語言中硬編碼的漂亮符號以方便人類使用。這里有幾個:
a -> b means (->) a b
[a] means [] a
(a,b) means (,) a b
(a,b,c) means (,,) a b c
... ditto for other tuple types
正因為如此,我們可以找到一個仿函式實體,例如instance Functor ((->) a)who fmaphas type
(x -> y) -> ((->) a x) -> ((->) a y)
也可以寫成
(x -> y) -> (a -> x) -> (a -> y)
這fmap只是函陣列合,即fmap = (.).
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412893.html
標籤:
