它看起來好像轉換應該形成一個Monoid,其中標識函數爲空元素,標準函數組合爲二元運算。我認爲這不是特別有用,但它應該是可能的。沿着線的東西:轉換集f :: a->一個形式在函數組合上的幺半羣 - 我該如何使這是一個Monoid的實例?
instance Monoid (a -> a) where
mempty = id
mappend f g = (.)
以上不編譯,可能是因爲它是由預先存在的定義屏蔽
instance Monoid b => Monoid (a -> b) where
mempty _ = mempty
mappend f g x = f x `mappend` g x
錯誤是:
Illegal instance declaration for ‘Monoid (a -> a)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monoid (a -> a)’
我米仍然是一個Haskell擦洗,所以我不知道我怎麼能解決這個問題 - 任何幫助?
作爲腳註中重複的答案,一個漂亮的使用'Endo'在[得到一個默認實現爲'foldr'出'foldMap'](的一個例子http://stackoverflow.com/q /2751851分之23319683)。 – duplode