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 (<<<) = (.) (&…
Identity Monad newtype Identity a = Identity { runIdentity :: a } instance Functor Identity where fmap = coerce instance Applicative Identity where pure = Identity (<*>) = coerce instance Monad Identity where m >>= k = k (runIdentity m) newtyp…
安装 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",…
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…
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…
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 &…
安装 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…
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…
安装 lens-tutorial Control.Lens.Tutorial $ cabal install lens-tutorial Installed lens-tutorial-1.0.3 Prelude> :m +Control.Lens.Tutorial Prelude Control.Lens.Tutorial> {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DeriveFoldable #-} {-# LANGUAGE Derive…
Bifoldable class Bifoldable p where bifold :: Monoid m => p m m -> m bifold = bifoldMap id id bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> p a b -> m bifoldMap f g = bifoldr (mappend . f) (mappend . g) mempty bifoldr :: (a ->…
Parser 类型 data ParsecT s u m a type Parsec s u = ParsecT s u Identity type Parser = Parsec String () data ParsecT s u m a ParsecT 带四个类型参数:数据流类型 s,用户状态类型 u,底层Monad类型 m,返回类型 a. ParsecT 是一个Monad转换器. type Parsec s u = ParsecT s u Identity Parsec 类型是 Pars…
手动计算(view, over, set, to, _1) view l = getConst . l Const over l f = runIdentity . l (Identity . f) set l b = runIdentity . l (\_ -> Identity b) to k = dimap k (contramap k) instance Field1 (a,b) (a',b) a a' where _1 k ~(a,b) = k a <&> \a' -&…
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…
Function, Monad, Arrow f :: Int -> (Int, Int) f = \x -> let y = 2 * x z1 = y + 3 z2 = y - 5 in (z1, z2) -- ghci> f 10 -- (23, 15) fM :: Int -> Identity (Int, Int) fM = \x -> do y <- return (2 * x) z1 <- return (y + 3) z2 <- return…