在阅读http://uncyclopedia.wikia.com/wiki/Haskell(并忽略所有"令人反感"的东西)时,我偶然发现了以下一段混淆代码:
fix$(<$>)<$>(:)<*>((<$>((:[{- thor's mother -}])<$>))(=<<)<$>(*)<$>(*2))$1
Run Code Online (Sandbox Code Playgroud)
当我在ghci(导入Data.Function和Control.Applicative)之后运行那段代码时,ghci打印所有2的幂的列表.
这段代码如何工作?
Dan*_*ner 213
首先,我们有一个可爱的定义
x = 1 : map (2*) x
Run Code Online (Sandbox Code Playgroud)
如果你以前从未见过它,它本身就有点令人费解.无论如何,这是一个相当标准的懒惰和递归技巧.现在,我们将使用fixand point-free-ify 摆脱显式递归.
x = fix (\vs -> 1 : map (2*) vs)
x = fix ((1:) . map (2*))
Run Code Online (Sandbox Code Playgroud)
接下来我们将要做的是扩展该:部分并使map不必要的复杂化.
x = fix ((:) 1 . (map . (*) . (*2)) 1)
Run Code Online (Sandbox Code Playgroud)
那么,现在我们有两个常量副本1.这将永远不会,所以我们将使用阅读器应用程序去重复它.此外,功能组合有点垃圾,所以让(<$>)我们随时随地取而代之.
x = fix (liftA2 (.) (:) (map . (*) . (*2)) 1)
x = fix (((.) <$> (:) <*> (map . (*) . (*2))) 1)
x = fix (((<$>) <$> (:) <*> (map <$> (*) <$> (*2))) 1)
Run Code Online (Sandbox Code Playgroud)
接下来:该调用map过于可读.但是没有什么可担心的:我们可以使用monad定律来扩展它.特别是fmap f x = x >>= return . f,如此
map f x = x >>= return . f
map f x = ((:[]) <$> f) =<< x
Run Code Online (Sandbox Code Playgroud)
我们可以指向free-ify,替换(.)为(<$>),然后添加一些虚假部分:
map = (=<<) . ((:[]) <$>)
map = (=<<) <$> ((:[]) <$>)
map = (<$> ((:[]) <$>)) (=<<)
Run Code Online (Sandbox Code Playgroud)
在上一步中用这个等式代替:
x = fix (((<$>) <$> (:) <*> ((<$> ((:[]) <$>)) (=<<) <$> (*) <$> (*2))) 1)
Run Code Online (Sandbox Code Playgroud)
最后,你打破空格键并产生精彩的最终方程式
x=fix(((<$>)<$>(:)<*>((<$>((:[])<$>))(=<<)<$>(*)<$>(*2)))1)
Run Code Online (Sandbox Code Playgroud)
小智 14
写了一个很长的答案,我的IRC日志的完整贯穿最终代码的实验(这是在2008年初),但我不小心所有的文本:)虽然没有那么多的损失 - 为大多数情况下丹尼尔的分析都是现实的.
这是我开始的:
Jan 25 23:47:23 <olsner> @pl let q = 2 : map (2*) q in q
Jan 25 23:47:23 <lambdabot> fix ((2 :) . map (2 *))
Run Code Online (Sandbox Code Playgroud)
差异主要归结为重构发生的顺序.
x = 1 : map (2*) x我开始2 : map ...,我保持最初的2直到最后一个版本,我挤压了一个(*2)并$2在最后改为$1."让地图不必要地复杂化"的步骤没有发生(那个早期).map在使用Applicative组合器替换liftM2之前,使用了混淆功能.当所有空间消失时也是如此..功能组合遗留下来.<$>在那个与非百科全书之间的几个月中,明显地发生了所有这些事件的替换.顺便说一句,这是一个更新的版本,不再提到这个数字2:
fix$(<$>)<$>(:)<*>((<$>((:[{- Jörð -}])<$>))(=<<)<$>(*)<$>(>>=)(+)($))$1
Run Code Online (Sandbox Code Playgroud)
这两个答案都是从突然给出的简短原始代码中得出混淆的代码片段,但问题实际上是问长混淆代码如何完成其工作。
就是这样:
fix$(<$>)<$>(:)<*>((<$>((:[{- thor's mother -}])<$>))(=<<)<$>(*)<$>(*2))$1
= {- add spaces, remove comment -}
fix $ (<$>) <$> (:) <*> ( (<$> ((:[]) <$>) ) (=<<) <$> (*) <$> (*2) ) $ 1
-- \__\______________/_____________________________/
= {- A <$> B <*> C $ 1 = A (B 1) (C 1) -}
fix $ (<$>) (1 :) ( ( (<$> ((:[]) <$>) ) (=<<) <$> (*) <$> (*2) ) 1 )
-- \__\______________/____________________________/
= {- (<$>) A B = (A <$> B) ; (<$> B) A = (A <$> B) -}
fix $ (1 :) <$> ( (((=<<) <$> ((:[]) <$>) ) <$> (*) <$> (*2) ) 1 )
-- \\____________________/____________________________/
= {- <$> is left associative anyway -}
fix $ (1 :) <$> ( ( (=<<) <$> ((:[]) <$>) <$> (*) <$> (*2) ) 1 )
-- \__________________________________________________/
= {- A <$> foo = A . foo when foo is a function -}
fix $ (1 :) <$> ( ( (=<<) <$> ((:[]) <$>) . (*) . (*2) ) 1 )
-- \__________________________________________________/
= {- ((:[]) <$>) = (<$>) (:[]) = fmap (:[]) is a function -}
fix $ (1 :) <$> ( ( (=<<) . ((:[]) <$>) . (*) . (*2) ) 1 )
-- \__________________________________________________/
= {- ( A . B . C . D) 1 = A (B (C (D 1))) -}
fix $ (1 :) <$> (=<<) ( ((:[]) <$>) ( (*) ( (*2) 1 )))
= {- (*2) 1 = (1*2) = 2 -}
fix $ (1 :) <$> (=<<) ( ((:[]) <$>) ( (*) 2 ))
= {- (*) 2 = (2*) -}
fix $ (1 :) <$> (=<<) ( ((:[]) <$>) (2*) )
= {- ( A <$>) B = A <$> B -}
fix $ (1 :) <$> (=<<) ( (:[]) <$> (2*) )
= {- A <$> foo = A . foo when foo is a function -}
fix $ (1 :) <$> (=<<) ( (:[]) . (2*) )
= {- (f . g) = (\ x -> f (g x)) -}
fix $ (1 :) <$> (=<<) (\ x -> [2*x] )
= {- (=<<) A = ( A =<<) -}
fix $ (1 :) <$> ( (\ x -> [2*x] ) =<<)
Run Code Online (Sandbox Code Playgroud)
这( (\ x -> [2*x]) =<<) = (>>= (\ x -> [2*x])) = concatMap (\ x -> [2*x]) = map (2*)是一个函数,所以再说一遍<$> = .:
=
fix $ (1 :) . map (2*)
= {- substitute the definition of fix -}
let xs = (1 :) . map (2*) $ xs in xs
=
let xs = 1 : [ 2*x | x <- xs] in xs
= {- xs = 1 : ys -}
let ys = [ 2*x | x <- 1:ys] in 1:ys
= {- ys = 2 : zs -}
let zs = [ 2*x | x <- 2:zs] in 1:2:zs
= {- zs = 4 : ws -}
let ws = [ 2*x | x <- 4:ws] in 1:2:4:ws
=
iterate (2*) 1
=
[2^n | n <- [0..]]
Run Code Online (Sandbox Code Playgroud)
是2的所有幂,按升序排列。
这使用
A <$> B <*> C $ x = liftA2 A B C x因为liftA2 A B Cis 应用于x它是一个函数, an 作为函数意味着liftA2 A B C x = A (B x) (C x)。
(f `op` g) = op f g = (f `op`) g = (`op` g) f是算子截面三定律
>>=是一元绑定,并且因为(`op` g) f = op f g和类型是
(>>=) :: Monad m => m a -> (a -> m b ) -> m b
(\ x -> [2*x]) :: Num t => t -> [ t]
(>>= (\ x -> [2*x])) :: Num t => [ t] -> [ t]
Run Code Online (Sandbox Code Playgroud)
通过类型应用和替换,我们看到所讨论的 monad 是[]for which (>>= g) = concatMap g。
concatMap (\ x -> [2*x]) xs 被简化为
concat $ map (\ x -> [2*x])
=
concat $ [ [2*x] | x <- xs]
=
[ 2*x | x <- xs]
=
map (\ x -> 2*x )
Run Code Online (Sandbox Code Playgroud)
根据定义,
(f . g) x = f (g x)
fix f = let x = f x in x
iterate f x = x : iterate f (f x)
= x : let y = f x in
y : iterate f (f y)
= x : let y = f x in
y : let z = f y in
z : iterate f (f z)
= ...
= [ (f^n) x | n <- [0..]]
Run Code Online (Sandbox Code Playgroud)
在哪里
f^n = f . f . ... . f
-- \_____n_times _______/
Run Code Online (Sandbox Code Playgroud)
以便
((2*)^n) 1 = ((2*) . (2*) . ... . (2*)) 1
= 2* ( 2* ( ... ( 2* 1 )...))
= 2^n , for n in [0..]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15053 次 |
| 最近记录: |