Haskell语言学习笔记(31)ListT
Control.Monad.Trans.List
标准库中的 ListT 的实现由于有 bug,已经被废弃。
list-t 模块
这里使用 list-t 模块中的 ListT。
list-t 模块需要安装
ListT done right
$ cabal install list-t
list-t-1.0.0.1
Prelude> :m +ListT
Prelude ListT>
ListT Monad转换器
newtype ListT m a = ListT (m (Maybe (a, ListT m a)))
-- * Execution in the inner monad
-------------------------
-- Execute in the inner monad,
-- getting the head and the tail.
-- Returns nothing if it's empty.
uncons :: ListT m a -> m (Maybe (a, ListT m a))
-- Execute, getting the head. Returns nothing if it's empty.
head :: Monad m => ListT m a -> m (Maybe a)
-- Execute, getting the tail. Returns nothing if it's empty.
tail :: Monad m => ListT m a -> m (Maybe (ListT m a))
-- Execute, checking whether it's empty.
null :: Monad m => ListT m a -> m Bool
-- Execute, applying a left fold.
fold :: Monad m => (r -> a -> m r) -> r -> ListT m a -> m r
-- A version of 'fold', which allows early termination.
foldMaybe :: Monad m => (r -> a -> m (Maybe r)) -> r -> ListT m a -> m r
-- Execute, folding to a list.
toList :: Monad m => ListT m a -> m [a]
-- Execute, folding to a list in the reverse order.
-- Performs more efficiently than 'toList'.
toReverseList :: Monad m => ListT m a -> m [a]
-- Execute, traversing the stream with a side effect in the inner monad.
traverse_ :: Monad m => (a -> m ()) -> ListT m a -> m ()
-- Execute, consuming a list of the specified length and returning the remainder stream.
splitAt :: Monad m => Int -> ListT m a -> m ([a], ListT m a)
-- * Construction
-------------------------
-- Prepend an element.
cons :: Monad m => a -> ListT m a -> ListT m a
-- Construct from any foldable.
fromFoldable :: (Monad m, Foldable f) => f a -> ListT m a
-- Construct by unfolding a pure data structure.
unfold :: Monad m => (b -> Maybe (a, b)) -> b -> ListT m a
-- Construct by unfolding a monadic data structure
-- This is the most memory-efficient way to construct ListT where
-- the length depends on the inner monad.
unfoldM :: Monad m => (b -> m (Maybe (a, b))) -> b -> ListT m a
-- Produce an infinite stream.
repeat :: Monad m => a -> ListT m a
-- * Transformation
-------------------------
-- A transformation,
-- which traverses the stream with an action in the inner monad.
traverse :: Monad m => (a -> m b) -> ListT m a -> ListT m b
-- A transformation,
-- reproducing the behaviour of @Data.List.'Data.List.take'@.
take :: Monad m => Int -> ListT m a -> ListT m a
-- A transformation,
-- reproducing the behaviour of @Data.List.'Data.List.drop'@.
drop :: Monad m => Int -> ListT m a -> ListT m a
-- A transformation,
-- which slices a list into chunks of the specified length.
slice :: Monad m => Int -> ListT m a -> ListT m [a]
Prelude ListT> toList (return 1 :: ListT IO Int)
[1]
Prelude ListT> toList (return 1 :: ListT Maybe Int)
Just [1]
Prelude ListT> toList (fromFoldable [1,2,3] :: ListT Maybe Int)
Just [1,2,3]
Prelude ListT Control.Monad.Trans> toList $ (return 1 :: ListT IO Int) >>= lift . print >> return (1,2)
1
[(1,2)]
应用实例
import Control.Monad
import Control.Monad.Trans
import ListT
myTest :: Int -> ListT IO (Int, Int)
myTest n = do
let squares = fromFoldable . takeWhile (<=n) $ map (^(2::Int)) [0..]
x <- squares
y <- squares
lift $ print (x,y)
guard $ x + y == n
lift $ putStrLn "Sum of squares."
return (x,y)
main = toList $ myTest 5
(0,0)
(0,1)
(0,4)
(1,0)
(1,1)
(1,4)
Sum of squares.
(4,0)
(4,1)
Sum of squares.
(4,4)
[(1,4),(4,1)]
Haskell语言学习笔记(31)ListT的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- Haskell语言学习笔记(44)Lens(2)
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...
- Haskell语言学习笔记(38)Lens(1)
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...
- Haskell语言学习笔记(92)HXT
HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...
随机推荐
- 三个php加密解密算法
三个功能强大的php加密解密函数 //加密函数 function lock_url($txt,$key='www.fyunw.com') { $chars = "ABCDEFGHIJKLMN ...
- 解决首次访问jenkins,输入初始化默认密码之后,一直卡住问题,无法进行jenkins工具安装
参考网址:http://www.cnblogs.com/520playboy/p/6244257.html 简介 安装系统:centos6.5 安装方式:在官网中下载jenkins.war,放到t ...
- Centos替换默认源
将默认的国外源替换为国内的网易的源 参考帮助文档:http://mirrors.163.com/.help/centos.html 查看本机版本:cat /etc/redhat-release 先安装 ...
- 使用Jquery实现Win8开始菜单效果的站点导航
前言: 本人是个Metro控,自我感觉到处都充斥着Metro的元素,个人认为这种风格强调表现以及内容,以简洁著称,不过也不是大部分都喜欢,也有一些人和你讨厌这种风格~不过本人非常喜欢这种风格,看我博客 ...
- wxWidgets:wxApp概述
在我们编写wxWidgets应用程序的时候,我们不需要为之定义一个main函数:不过我们需要实现wxApp派生类的一个成员函数OnInit,它的地位大致等价于一般C++程序中的main. 一般来说On ...
- 在react-native中使用es7语法中的decorator装饰器
在react-native中默认使用decorator会红屏报错,需要安装一个babel插件: babel-plugin-transform-decorators-legacy 然后在根目录下的.ba ...
- Apache2.4.7 + php5 + mysql thinkphp
1. LAMP 的安装sudo apt-get install apache2 2.安装PHP sudo apt-get install libapache2-mod-php5 php5 php5- ...
- GC之五--SystemGC完全解读
目录: GC之一--GC 的算法分析.垃圾收集器.内存分配策略介绍 GC之二--GC日志分析(jdk1.8)整理中 GC之三--GC 触发Full GC执行的情况及应对策略 gc之四--Minor G ...
- ESXI5.5设置主机的时间自动同步服务 NTP
背景:现在公司的很多线上服务也都通过虚拟化来实现,最近遇到一个小问题,虚拟机上的时间不准确.原来是虚拟机会主动同步宿主机时间,一般虚拟机中都安装vmware tool工具,这个工具会自动和宿主机进行时 ...
- uploadify是通过flash上传,服务器获取type为application/octet-stream
uploadify是通过flash上传,服务器获取type为application/octet-stream,因此允许上传的类型要加上application/octet-stream