Data.Maybe

Prelude Data.Maybe> maybe "" show (Just 5)
"5"
Prelude Data.Maybe> maybe "" show Nothing
""
Prelude Data.Maybe> isJust (Just 3)
True
Prelude Data.Maybe> isJust Nothing
False
Prelude Data.Maybe> isNothing (Just 3)
False
Prelude Data.Maybe> isNothing Nothing
True
Prelude Data.Maybe> fromJust (Just 1)
1
Prelude Data.Maybe> 2 * (fromJust Nothing)
*** Exception: Maybe.fromJust: Nothing
Prelude Data.Maybe> fromMaybe "" (Just "Hello, World!")
"Hello, World!"
Prelude Data.Maybe> fromMaybe "" Nothing
""
Prelude Data.Maybe> listToMaybe []
Nothing
Prelude Data.Maybe> listToMaybe [1,2,3]
Just 1
Prelude Data.Maybe> maybeToList (Just 7)
[7]
Prelude Data.Maybe> maybeToList Nothing
[]
Prelude Data.Maybe> catMaybes [Just 1, Nothing, Just 3]
[1,3]
Prelude Data.Maybe> let readMaybeInt = Text.Read.readMaybe :: String -> Maybe Int
Prelude Data.Maybe> mapMaybe readMaybeInt ["1", "Foo", "3"]
[1,3]

Data.Either

Prelude Data.Either> either length (*2) $ Left "foo"
3
Prelude Data.Either> either length (*2) $ Right 3
6
Prelude Data.Either> lefts [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
["foo","bar","baz"]
Prelude Data.Either> rights [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
[3,7]
Prelude Data.Either> isLeft (Left "foo")
True
Prelude Data.Either> isLeft (Right 3)
False
Prelude Data.Either> isRight (Left "foo")
False
Prelude Data.Either> isRight (Right 3)
True
Prelude Data.Either> fromLeft 1 (Left 3)
3
Prelude Data.Either> fromLeft 1 (Right "foo")
1
Prelude Data.Either> fromRight 1 (Right 3)
3
Prelude Data.Either> fromRight 1 (Left "foo")
1
Prelude Data.Either> partitionEithers [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
(["foo","bar","baz"],[3,7])

Haskell语言学习笔记(32)Data.Maybe, Data.Either的更多相关文章

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

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

  2. Haskell语言学习笔记(76)Data.Tree

    Data.Tree data Tree a = Node { rootLabel :: a, subForest :: Forest a } deriving (Eq, Read, Show) typ ...

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

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

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

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

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

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

  6. Haskell语言学习笔记(77)Data.HashSet

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

  7. Haskell语言学习笔记(65)Data.HashMap

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

  8. Haskell语言学习笔记(55)Data.Vector

    Data.Vector Construction Prelude V> import Data.Vector as V Prelude V> V.empty [] Prelude V> ...

  9. Haskell语言学习笔记(54)Data.Set

    Data.Set Prelude> import Data.Set as Set Prelude Set> :set -XOverloadedLists Construction Prel ...

  10. Haskell语言学习笔记(53)Data.Sequence

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

随机推荐

  1. 箭头函数中的 this

    JS 每一个 function 都有自己独立的运行上下文,但箭头函数不属于普通的 function,所以没有独立的上下文. 所以在箭头函数里写的 this 其实是包含该箭头函数最近的一个 functi ...

  2. chrome 小技巧:保持元素的hover状态

    审查元素,选中需要hover的标签 点击"Styles"菜单中的":hov",弹出 Force element state 选中相应的 :hover :acti ...

  3. button使用注意

    <button type="button" class="btn btn-primary" onclick="ChangePassword(); ...

  4. TopCoder客户端安装

    参考:https://blog.csdn.net/github_39353095/article/details/76165940 首先,下载 Java 环境. https://www.java.co ...

  5. 【android】adb常用命令

    ADB常用命令: [adb help]获取帮助 [adb get-serialno]获取设备串号 [adb -s <serialNumber> <command>]给特定设备发 ...

  6. Linux strace命令 一

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  7. 服务端REST与SOAP的探讨(转)

    声明: 闲来逛论坛看到一篇不错的文章,阅读后受益匪浅. 本文从一个简单的应用场景出发,使用REST和SOAP两种不同的架构风格实现,通过对REST与SOAP Web服务具体对比,旨在帮助读者更深刻理解 ...

  8. vim应用:终极解决windows系统gvim/vim的各种乱码(文件,菜单,提示信息)!

    这个方法解决了我的windows下 gvim的中文乱码问题(跟大家分享一下). 此方法引用   http://www.douban.com/note/145491549/ 查看文件的编码::echo ...

  9. Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

    转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...

  10. 学习笔记之YAML

    The Official YAML Web Site http://yaml.org/ YAML(tm) is an international collaboration to make a dat ...