Data.Sequence

Prelude> import Data.Sequence as Seq
Prelude Seq> :set -XOverloadedLists
Prelude Seq>

Construction

Prelude Seq> empty
fromList []
Prelude Seq> singleton 3
fromList [3]
Prelude Seq> 3 <| [4,5]
fromList [3,4,5]
Prelude Seq> [4,5] |> 6
fromList [4,5,6]
Prelude Seq> [3,4] >< [5,6]
fromList [3,4,5,6]

Repetition

Prelude Seq> Seq.replicate 3 3
fromList [3,3,3]
Prelude Seq> Seq.replicateA 3 (Just 3)
Just [3,3,3]
Prelude Seq> Seq.replicateM 3 (Just 3)
Just [3,3,3]
Prelude Seq> cycleTaking 9 [3,4,5]
fromList [3,4,5,3,4,5,3,4,5]

Iterative construction

Prelude Seq> iterateN 5 (+1) 1
fromList [1,2,3,4,5]
Prelude Seq> unfoldr (\b -> if b < 10 then Just (b, b+1) else Nothing) 1
fromList [1,2,3,4,5,6,7,8,9]

Queries

Prelude Seq> Seq.null []
True
Prelude Seq> Seq.length [1,2,3]
3

Views

Prelude Seq> viewl [1,2,3]
1 :< fromList [2,3]
Prelude Seq> viewr [1,2,3]
fromList [1,2] :> 3

Scans

Prelude Seq> Seq.scanl (+) 0 [1,2,3]
fromList [0,1,3,6]
Prelude Seq> Seq.scanl1 (+) [1,2,3]
fromList [1,3,6]
Prelude Seq> Seq.scanr (+) 0 [1,2,3]
fromList [6,5,3,0]
Prelude Seq> Seq.scanr1 (+) [1,2,3]
fromList [6,5,3]

Sublists

Prelude Seq> Seq.tails [1..6]
fromList [fromList [1,2,3,4,5,6],fromList [2,3,4,5,6],fromList [3,4,5,6],fromList [4,5,6],fromList [5,6],fromList [6],fromList []]
Prelude Seq> Seq.inits [1..6]
fromList [fromList [],fromList [1],fromList [1,2],fromList [1,2,3],fromList [1,2,3,4],fromList [1,2,3,4,5],fromList [1,2,3,4,5,6]]
Prelude Seq> chunksOf 2 [1..6]
fromList [fromList [1,2],fromList [3,4],fromList [5,6]]
Prelude Seq> takeWhileL even [2,4,5,6,8]
fromList [2,4]
Prelude Seq> takeWhileR even [2,4,5,6,8]
fromList [6,8]
Prelude Seq> dropWhileL even [2,4,5,6,8]
fromList [5,6,8]
Prelude Seq> dropWhileR even [2,4,5,6,8]
fromList [2,4,5]
Prelude Seq> spanl even [2,4,5,6,8]
[2,4],fromList [5,6,8]
Prelude Seq> spanr even [2,4,5,6,8]
[6,8],fromList [2,4,5]
Prelude Seq> breakl even [2,4,5,6,8]
[],fromList [2,4,5,6,8]
Prelude Seq> breakr even [2,4,5,6,8]
[],fromList [2,4,5,6,8]
Prelude Seq> Seq.partition even [2,4,5,6,8]
[2,4,6,8],fromList [5]
Prelude Seq> Seq.filter even [2,4,5,6,8]
fromList [2,4,6,8]

Sorting

Prelude Seq Data.Function> Seq.sort ["one","two","three","four"]
fromList ["four","one","three","two"]
Prelude Seq Data.Function> Seq.sortBy (compare `on` Prelude.length) ["one","two","three","four"]
fromList ["one","two","four","three"]
Prelude Seq Data.Function> unstableSort ["one","two","three","four"]
fromList ["four","one","three","two"]
Prelude Seq Data.Function> unstableSortBy (compare `on` Prelude.length) ["one","two","three","four"]
fromList ["one","two","four","three"]

Indexing

Prelude Seq> Seq.lookup 3 [1..6]
Just 4
Prelude Seq> [1..6] !? 3
Just 4
Prelude Seq> [1..6] `index` 3
4
Prelude Seq> adjust (+1) 3 [1..6]
fromList [1,2,3,5,5,6]
Prelude Seq> update 3 1 [1..6]
fromList [1,2,3,1,5,6]
Prelude Seq> Seq.take 3 [1..6]
fromList [1,2,3]
Prelude Seq> Seq.drop 3 [1..6]
fromList [4,5,6]
Prelude Seq> insertAt 3 3 [1..6]
fromList [1,2,3,3,4,5,6]
Prelude Seq> deleteAt 3 [1..6]
fromList [1,2,3,5,6]
Prelude Seq> Seq.splitAt 3 [1..6]
[1,2,3],fromList [4,5,6]
Prelude Seq> elemIndexL 3 [1,2,3,3,3,4,5]
Just 2
Prelude Seq> elemIndexL 3 [1,2,3,3,6,3,3,4,5]
Just 2
Prelude Seq> elemIndicesL 3 [1,2,3,3,6,3,3,4,5]
[2,3,5,6]
Prelude Seq> elemIndexR 3 [1,2,3,3,6,3,3,4,5]
Just 6
Prelude Seq> elemIndicesR 3 [1,2,3,3,6,3,3,4,5]
[6,5,3,2]
Prelude Seq> findIndexL even [1,2,3,3,6,3,3,4,5]
Just 1
Prelude Seq> findIndicesL even [1,2,3,3,6,3,3,4,5]
[1,4,7]
Prelude Seq> findIndexR even [1,2,3,3,6,3,3,4,5]
Just 7
Prelude Seq> findIndicesR even [1,2,3,3,6,3,3,4,5]
[7,4,1]

Folds

Prelude Seq> foldMapWithIndex (\n a -> fmap (+n) a) [[1],[2]]
[1,3]
Prelude Seq> foldlWithIndex (\a b c -> a+b+c) 0 [1..3]
9
Prelude Seq> foldrWithIndex (\a b c -> a+b+c) 0 [1..3]
9

Transformations

Prelude Seq> mapWithIndex (+) [1..3]
fromList [1,3,5]
Prelude Seq> traverseWithIndex (\a b -> [b+a,b+a+1]) [1..3]
[fromList [1,3,5],fromList [1,3,6],fromList [1,4,5],fromList [1,4,6],fromList [2,3,5],fromList [2,3,6],fromList [2,4,5],fromList [2,4,6]]
Prelude Seq> Seq.reverse [1..4]
fromList [4,3,2,1]
Prelude Seq> Seq.intersperse 9 [1..3]
fromList [1,9,2,9,3]

Zips

Prelude Seq> Seq.zip [1..3] [1..3]
fromList [(1,1),(2,2),(3,3)]
Prelude Seq> Seq.zipWith (*) [1..3] [1..3]
fromList [1,4,9]

Haskell语言学习笔记(53)Data.Sequence的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(28)Data.Map

    Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...

  7. Haskell语言学习笔记(48)Data.Tuple

    Data.Tuple fst :: (a,b) -> a fst (x,_) = x snd :: (a,b) -> b snd (_,y) = y curry :: ((a, b) -& ...

  8. Haskell语言学习笔记(93)Data.Text

    Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...

  9. Haskell语言学习笔记(81)Data.Typeable

    Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...

随机推荐

  1. linux SSH各配置项解释

    Linux下SSH各配置项解释 关于ssh 设置的相关总结(ssh最大连接数.ssh连接时长.安全性配置等)以redhat6.3为例 ssh配置文件在: /etc/ssh/sshd_config可以打 ...

  2. 豆瓣源安装python包

    例如安装scrapy: pip install -i https://pypi.douban.com/simple/ scrapy

  3. <<APUE>> 编译方法

    /********************************************************************************第0种-最简单实用********** ...

  4. serialize unserialize

    转自 http://www.cnblogs.com/yeer/archive/2009/03/25/1421161.html php函数serialize()与unserialize()   seri ...

  5. java 中getDeclaredFields() 与getFields() 的区别

    java 中getDeclaredFields() 与getFields() 的区别 getDeclaredFields()返回Class中所有的字段,包括私有字段.例证: package com.t ...

  6. 关于后台程序java报错问题解决

    HTTP Status – Internal Server Error Type Exception Report Message Handler processing failed; nested ...

  7. canvas基础之变换

    2D绘制上下文支持各种基本的绘制变换: rotate(angle):围绕原点旋转图像angle弧度.(举例:如需旋转 5 度,可规定下面的公式:5*Math.PI/180.) scale(scaleX ...

  8. gradle使用心得

    gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...

  9. python3 tkinter

    https://morvanzhou.github.io/tutorials/python-basic/tkinter/ 李导师推荐的.非常感谢.非常棒的视频教程!!! 只不过里面的教程视频是yout ...

  10. 使用oracle导出的dmp文件(包含表结构还是表数据?)

    我们都知道oracle提供了一个exp程序,可以导出dmp文件,那么dmp文件中到底包含哪些东西呢? 1:有对象的信息吗?比如对象的权限? 2:有表空间信息吗? 3:有表结构吗? 4:有表的索引和触发 ...