【问题标题】:Anamorphism in Banana, Lenses and Barbed wires香蕉、镜头和铁丝网中的变形
【发布时间】:2017-10-30 13:57:21
【问题描述】:

不应该

g a = (a,a)iterate 的变形定义中?我发现f 不适用于zip 案例。还是我看错了?

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.41.125&rep=rep1&type=pdf

【问题讨论】:

    标签: haskell functional-programming


    【解决方案1】:

    不,g a = (a, f a) 是正确的定义。我不确定我是否理解您对zip 的反对意见; iteratezip 这两个函数并不真正相关。在本文中,它们都作为变形实现,但相似之处基本上到此为止——除了变形的定义之外,它们不共享任何代码。

    您可以通过将他的数学转录到 Haskell 来验证这些定义是否正确:

    import Prelude hiding (iterate)
    
    anamorphism g p b | p b = []
                      | otherwise = a : anamorphism g p b'
        where (a, b') = g b
    
    iterate f = anamorphism g (const False)
        where g a = (a, f a)
    

    然后,在 ghci 中:

    > take 10 (iterate succ 0)
    [0,1,2,3,4,5,6,7,8,9]
    

    这与内置的:

    > take 10 (Prelude.iterate succ 0)
    [0,1,2,3,4,5,6,7,8,9]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 2011-12-12
      相关资源
      最近更新 更多