第一个问题,这里面完全在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
我想用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