Haskell语言学习笔记(11)Point-Free

来源:互联网 发布:openwrt 修改管理端口 编辑:程序博客网 时间:2024/05/14 02:29
本文内容来自 "Point-Free or Die: Tacit Programming in Haskell and Beyond" by Amar Shah

Point-Free

\x -> outside (inside x)\x -> outside $ inside x\x -> outside . inside $ x      outside . inside

aggregate

totalNumber = sum . map lengthtotalNumber = aggregate length    where aggregate f = sum . map f

eta-abstraction

An eta-abstraction is adding of abstraction over a function.
aggregate f    = sum . map faggregate f xs = sum . map f $ xsaggregate f xs = sum $ map f xsaggregate f xs = sum (map f xs)

eta-reduction

An eta-reduction is dropping of abstraction over a function.
aggregate f = sum . map faggregate f = (.) sum (map f)aggregate f = (.) sum $ map faggregate f = (.) sum . map $ faggregate   = \f -> (.) sum . map $ faggregate   = (.) sum . mapaggregate   = (sum .) . map

combinator

A combinator is a function with no free variables, that is, a pure lambda-expression that refers only to its arguments.
\f g     -> (f .) . g\f g x   -> (f .) . g $ x\f g x   -> (f .) $ g x\f g x   -> (f .) (g x)\f g x   -> f . g x\f g x y -> f . g x $ y\f g x y -> f $ g x y\f g x y -> f (g x y)

黑鸟(blackbird)组合运算符 (...)

The blackbird combinator is the composition of composition and composition.
f ... g = (f .) . gf ... g = \x y -> f (g x y)f ... g = \x y -> f $ g x yf ... g = \x y -> f . g x $ yf ... g = \x   -> (.) f (g x)f ... g = \x   -> (.) f $ g xf ... g = \x   -> (.) f . g $ xf ... g =         (.) f . gf ... g =         (f .) . g(...) = (.) . (.)(...) = \f g -> (f .) . g(...) = \f g -> (.) f . g(...) = \f g -> (.) (f .) $ g(...) = \f   -> (.) (f .)(...) = \f   -> (.) ((.) f)(...) = \f   -> (.) $ (.) f(...) = \f   -> (.) . (.) $ f(...) =         (.) . (.)aggregate f xs = sum (map f xs)aggregate = sum ... map

(...) 与 (.)

f  .  g = \x   -> f (g x)f ... g = \x y -> f (g x y)f ... g = (f .) . g(...) = (.) . (.)
0 0