国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Haskell語言學(xué)習(xí)筆記(14)Foldable

2019-11-06 06:23:55
字體:
供稿:網(wǎng)友

Foldable

class Foldable t where    fold :: Monoid m => t m -> m    fold = foldMap id    foldMap :: Monoid m => (a -> m) -> t a -> m    foldMap f = foldr (mappend . f) mempty    foldr :: (a -> b -> b) -> b -> t a -> b    foldr' :: (a -> b -> b) -> b -> t a -> b    foldl :: (b -> a -> b) -> b -> t a -> b    foldl' :: (b -> a -> b) -> b -> t a -> b    foldr1 :: (a -> a -> a) -> t a -> a    foldl1 :: (a -> a -> a) -> t a -> a    toList :: t a -> [a]    null :: t a -> Bool    length :: t a -> Int    elem :: Eq a => a -> t a -> Bool    maximum :: forall a . Ord a => t a -> a    minimum :: forall a . Ord a => t a -> a    sum :: Num a => t a -> a    PRoduct :: Num a => t a -> a

Foldable Tree

import qualified Data.Foldable as Fimport Data.Monoiddata Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)instance F.Foldable Tree where    foldMap f Empty = mempty    foldMap f (Node x l r) = F.foldMap f l `mappend`                             f x           `mappend`                             F.foldMap f r                             testTree = Node 5              (Node 3                  (Node 1 Empty Empty)                  (Node 6 Empty Empty)              )              (Node 9                  (Node 8 Empty Empty)                  (Node 10 Empty Empty)              )main = do    -- print $ F.foldl (+) 0 testTree    print $ F.sum testTree    -- print $ F.foldl (*) 1 testTree    print $ F.product testTree    print $ getAny $ F.foldMap (/x -> Any $ x == 3) testTree    print $ getAny $ F.foldMap (/x -> Any $ x > 15) testTree    -- print $ F.foldMap (/x -> [x]) testTree    print $ F.toList testTree    print $ F.length testTree    print $ F.maximum testTree    print $ F.minimum testTree    print $ 3 `F.elem` testTree    print $ F.null testTree      {-4264800TrueFalse[1,3,6,5,8,9,10]TrueFalse-}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 绥江县| 宜城市| 柳林县| 新乡县| 错那县| 湘潭县| 株洲县| 宕昌县| 深水埗区| 竹山县| 礼泉县| 离岛区| 车险| 景东| 永仁县| 吉隆县| 溧水县| 东阳市| 桦川县| 通河县| 通海县| 郁南县| 武义县| 黄平县| 门头沟区| 常州市| 福州市| 邵东县| 闸北区| 崇明县| 新源县| 卓资县| 台湾省| 台前县| 长治市| 栾川县| 南投市| 元阳县| 元阳县| 夏邑县|