【问题标题】:Taylor sequence for the exponential function in HaskellHaskell中指数函数的泰勒序列
【发布时间】:2017-06-22 02:01:57
【问题描述】:

我尝试为指数函数实现泰勒序列,但我得到了大量错误,我不完全理解,因为所有代码段本身都可以工作......有人可以解释错误和解决方法吗请:

top x = map (x^) [0..]
bottom = foldr (*) 1 [1..]
whole x = zipWith (/) (top x) bottom

提前致谢!

【问题讨论】:

  • 可能是我累了,我今天工作了 10 个小时,所以这可能只是一些非常愚蠢的事情,如果是,请原谅
  • 您的问题似乎不完整,它与Taylor series 有什么关系?您可能需要添加更完整的 (MCVE) 代码。
  • 泰勒序列是计算指数函数 / sin / cos 等的序列,如问题中所述,我的代码与指数函数 e^x 的泰勒序列有关。我以为这是常识,如果不是这样,我很抱歉。不过感谢您的宝贵时间

标签: haskell taylor-series


【解决方案1】:

好的,我想通了。问题在于 bottom 实际上并不是所有可能的阶乘的列表。

为了解决这个问题,我不得不使用scanl 而不是foldr:

bottom = scanl (*) 1 [1..]

【讨论】:

    【解决方案2】:

    您可以在 GHCi 中分别测试每个函数,看看它是否与您设计的相同。

    比如你对bottom的第一个定义

    Prelude> bottom = foldr (*) 1 [1..]
    Prelude> :t bottom
    bottom :: (Num b, Enum b) => b
    

    没有给出列表。这显然不是zipWith 的正确输入

    Prelude> :t zipWith
    zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    

    与正确的比较

    Prelude> bottom = scanl (*) 1 [1..]
    Prelude> :t bottom
    bottom :: (Num b, Enum b) => [b]
    

    给出一个列表。

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2014-05-28
      相关资源
      最近更新 更多