我想用foldr或foldMap實現最小值。據演習中,它應該有這樣的定義: mini :: (Foldable t, Ord a) => t a -> Maybe a -- named "mini" to avoid name clash
這聽起來很簡單,但我不知道我能substiture下面X,使其工作。請幫助? mini xs = Just (foldr min X xs)
而且您還可
我想要實現可摺疊的 data Constant a b = Constant a
這是我簡單的嘗試: instance Foldable (Constant a) where
foldr f b (Constant a) = f a b
編譯錯誤我想了解的部分是: Couldn't match expected type ‘a1’ with actual type ‘a’
‘a
在哈斯克爾,我們有traverse_,上Foldable類型工程的結構與應用性功能倍,丟棄任何結果的函數: traverse_ :: (Applicative f,Foldable t) => (a -> f b) -> t a -> f()
因此,我們可以比如做到這一點: traverse_ putStrLn ["Hello, ","world!"]
traverse_ print (Ju
我有以下代碼: {-# LANGUAGE NoImplicitPrelude, OverloadedStrings, TypeFamilies #-}
module AI.Analysis.Rules where
import ClassyPrelude
-- Our set of rules
data RuleSet a = RuleSet [Rule a] [Rule a]
我想定義上Foldable類型 safeIndex :: (Foldable t, Integral i) => t a -> i -> Maybe a
safeIndex = foldr step (const Nothing)
where
step :: Integral i => a -> (i -> Maybe a) -> i -> Maybe a
step
我們知道fmap是fmap :: Functor f => (a -> b) -> f a -> f b和sum是sum :: (Num a, Foldable t) => t a -> a,但下面的代碼混淆了我。 > :t (fmap sum Just)
(fmap sum Just) :: Num b => b -> b
> fmap sum Just 3
3
爲什麼?
我有關於用於在Haskell使用與foldl類型定義問題使用與foldl,我有類型: data Client = GovOrg String
| Company String Integer Person String
| Individual Person Bool
deriving Show
data Person = Person String
第一個問題,這裏面完全在Haskell這樣的小白,所以請與我是那種:) 我與this哈斯克爾的問題,6號打練習 並最終來到了溶液(或類似的東西,我希望)與此代碼 combinations gr lis = filter clean $ sequence $ replicate gr lis
where
clean string
| total > gr = False
我實現可摺疊,數據結構如下: data Tree a = Leaf a | Node (Tree a) (Tree a) deriving Show
當我執行倍和foldMap: instance Foldable Tree where
--fold :: Monoid a => Tree a -> a
fold (Leaf x) = x
fold (Node l r) = fold