Haskell语言学习笔记(90)Default】的更多相关文章

ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Existentials GADTs {-# LANGUAGE GADTs #-} 广义抽象代数类型专用语言扩展 Haskell语言学习笔记(74)GADTs OverloadedStrings {-# LANGUAGE OverloadedStrings #-} 用于重载字符串字面量类型的语言扩展 H…
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以及递归的形式系统. lambda项 lambda演算由 lambda 项的语言构成.基本的 lambda 项只包含以下三种: 语法 名称 描述 Haskell语言中的相应表述 a 变量 表示参数或数学/逻辑值的字符或字符串 a (λx.M) 抽象化 函数定义(M是一个lambda项).变量x在表达式…
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安装失败) $ cabal install yesod Installed yesod-1.4.5 Hello World -- helloworld.hs {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANG…
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 modifyIORef 引用以及修改值的函数 修改引用的值 Prelude> :m +Data.IORef Prelude Data.IORef> a <- newIORef 1 Prelude Data.IORef> v <- readIORef a Prelude Data.I…
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance Category (->) where id = GHC.Base.id (.) = (GHC.Base..) (<<<) :: Category cat => cat b c -> cat a b -> cat a c (<<<) = (.) (&…
安装 data-default-class $ cabal install data-default-class Installed data-default-class-0.1.2.0 Prelude> :m +Data.Default.Class Prelude Data.Default.Class> data-default-class 这个库为类型提供了缺省值. Prelude Data.Default.Class> def :: Int 0 Prelude Data.Defau…
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Free f a)) instance Functor f => Functor (Free f) where fmap f = go where go (Pure a) = Pure (f a) go (Free fa) = Free (go <$> fa) instance Functor f…
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHaskell is needed for makeLenses; RankNTypes is needed for -- a few type signatures later on. {-# LANGUAGE TemplateHaskell, RankNTypes #-} import Control…
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens Prelude Control.Lens> view _1 ("abc", "def") "abc" Prelude Control.Lens> over _1 (++ "!!!") ("abc",…
HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude> :m +Text.XML.HXT.Parser.XmlParsec Prelude Text.XML.HXT.Parser.XmlParsec> Prelude Text.XML.HXT.Parser.XmlParsec> xread "<foo>abc<bar/…
Comprehension Extensions 关于解析式的相关语言扩展. List and Comprehension Extensions 24 Days of GHC Extensions: List Comprehensions ParallelListComp Prelude> [(w,x,y,z) | ((w,x),(y,z)) <- zip [(w,x) | w <- [1..3], x <- [2..4]] [(y,z) | y <- [3..5], z &…
String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串,注意这个函数的返回类型是多态的. Prelude> :m +Text.Printf Prelude Text.Printf> printf "%s, %d, %.4f\n" "hello" 123 pi hello, 123, 3.1416 Prelude…
安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> IO (Async a) 启动新线程,执行异步操作. wait :: Async a -> IO a 等待异步操作完成,并返回值. concurrently :: IO a -> IO b -> IO (a, b) 并发(concurrently)运行两个 IO 操作,返回两个结果. 示例…
Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Concurrent 模块属于标准库,不需要安装. forkIO threadDelay data ThreadId 代表线程句柄的类型. forkIO :: IO () -> IO ThreadId 创建线程返回线程句柄,在新的线程中运行指定的 IO 操作. threadDelay :: Int ->…
req req 是一个好用,类型安全,可扩展,上层的HTTP客户端的库. $ cabal install req Installed req-1.1.0 Prelude> :m +Network.HTTP.Req Prelude Network.HTTP.Req> 官方示例 {-# LANGUAGE OverloadedStrings, DeriveGeneric #-} import Control.Monad import Control.Monad.IO.Class import Dat…
fix 函数 fix 是一个在 Data.Function 模块中定义的函数,它是对于递归的封装,可以用于定义不动点函数. fix :: (a -> a) -> a fix f = let x = f x in x fix 函数的定义使用了递归绑定,比较难以理解: fix f = let x = f x in x = let x = f x in f x = let x = f x in f (f x) = let x = f x in f (f (f x)) = let x = f x in…
Existentials(存在类型) Existentially quantified types(Existentially types,Existentials)是一种将一组类型归为一个类型的方式. 通常在使用 type, newtype, data 定义新类型的时候,出现在右边的类型参数必须出现在左边. 存在类型可以突破此限制. 实例 {-# LANGUAGE ExistentialQuantification #-} data ShowBox = forall s. Show s =>…
Data.ByteString String 是 [Char] 的同义词,在使用上存在List的惰性所带来的性能问题. 在处理大型二进制文件时,可以使用 ByteString 来代替 String. ByteString 类型分为以下两种: Lazy 模块 Data.ByteString.Lazy 中的 Data.ByteString.Lazy.ByteString Lazy 模块内部使用 chunks(64K数据块). Strict 模块 Data.ByteString 中的 Data.Byt…
Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> OverloadedLists GHC 提供了语言扩展 OverloadedLists. 不使用这个语言扩展,所有的列表字面量都属于 [] 类型. 如果使用这个语言扩展,那么所有的列表字面量就都属于 IsList l => l 类型. Map, Set, Vector, Text, Array 都是 IsList…
Comonad class Functor w => Comonad w where extract :: w a -> a duplicate :: w a -> w (w a) duplicate = extend id extend :: (w a -> b) -> w a -> w b extend f = fmap f . duplicate Comonad 是个类型类. 比较 Monad 和 Comonad class Functor m => Mon…
组合子 1 Prelude Text.Parsec Text.Parsec.String> parseTest (count 3 (char 'a')) "aaa" "aaa" Prelude Text.Parsec Text.Parsec.String> parseTest (between (char '(') (char ')') anyChar) "(3)" '3' Prelude Text.Parsec Text.Parse…
Parsec Parsec是一个词法及语法分析器. 匹配字符与字符串 Prelude Text.Parsec> parseTest anyChar "a" 'a' Prelude Text.Parsec> parseTest (char 'a') "a" 'a' Prelude Text.Parsec> parseTest (satisfy (=='a')) "a" 'a' Prelude Text.Parsec> par…
MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a instance MonadCont (ContT r m) where callCC = ContT.callCC class Monad m => MonadCont m where MonadState 是个类型类,它为 ContT 等封装了CPS函数的 Monad 定义了通用接口. MonadSta…
Endo Monoid newtype Endo a = Endo { appEndo :: a -> a } instance Monoid (Endo a) where mempty = Endo id Endo f `mappend` Endo g = Endo (f . g) Endo 是个 newtype,也就是对现有类型的封装. Endo a 封装的是一个自反射的函数,即 a->a.通过 appEndo 字段可以取出这个函数. Endo a 在结合时结合两个函数,因此它本质上是对函…
MonadState 类型类 class Monad m => MonadState s m | m -> s where get :: m s get = state (\s -> (s, s)) put :: s -> m () put s = state (\_ -> ((), s)) state :: (s -> (a, s)) -> m a state f = do s <- get let ~(a, s') = f s put s' return…
MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -> m a writer ~(a, w) = do tell w return a tell :: w -> m () tell w = writer ((),w) listen :: m a -> m (a, w) pass :: m (a, w -> w) -> m a lis…
MonadReader 类型类 class Monad m => MonadReader r m | m -> r where ask :: m r ask = reader id local :: (r -> r) -> m a -> m a reader :: (r -> a) -> m a reader f = do r <- ask return (f r) instance Monad m => MonadReader r (ReaderT…
Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text.Read> decimal "123" Right (123,"") Prelude Data.Text.Read> decimal "abc" Left "input does not start with a digit&…
unicode-show $ cabal install unicode-show Installed unicode-show-0.1.0.2 Prelude> :m +Text.Show.Unicode Prelude Text.Show.Unicode> unicode-show 这个库可用于向控制台窗口输出 Unicode 字符. Prelude Text.Show.Unicode> ("Хорошо!",["哈斯克尔7.6.1的力量",&…
安装 time $ cabal install time Installed time-1.9.1 Prelude> import Data.Time Prelude Data.Time> time 是一个关于时间(日,时,时区,日历)的库. 示例 Prelude Data.Time> fromGregorianValid 2008 10 22 Just 2008-10-22 Prelude Data.Time> fromGregorianValid 2014 2 31 Nothi…