【问题标题】:Haskell, recursive call, many argumentsHaskell,递归调用,许多参数
【发布时间】:2016-04-13 23:03:04
【问题描述】:

我在实现一个应该多次调用自身的函数时遇到了一些麻烦。每次它更新它的值并提高它的准确性。在这个过程中它调用了其他函数,但我知道它会一直工作到:

Type = [Double] 
*All lists; including type: Same, but not predetermined length. 


f0 :: Double -> [[Double]] -> [Double] -> Type -> Type  

函数调用的典型例子:

f0 1.0 [[1.0, 0.5, 1.0], [1.0, -1.0, 1.0]] [1.0, 0.0] (initTypeLength 2)

我想做什么:

f1 :: Int -> Double -> [[Double]] -> [Double] -> Type -> Type  

... 其中“Int” (i) 调用迭代次数,递归地使用 f0,保持所有参数相同,但类型除外,该类型已更新。

不确定 f0 是否相关,但以防万一:

f0 a (x:xs) (y:ys) type = f_other a xs ys
                         $ f_yet_another a x y type 

让我感到困惑的是在何处以及如何陈述论点。

总结一下:这里的一切都是静态的,除了一个列表; “类型”,每次迭代都会更新。我怎么称呼这个?我觉得这很明显,但我似乎无法以haskell理解的方式弄清楚。

【问题讨论】:

    标签: haskell recursion iteration


    【解决方案1】:

    我想你想要的是这样的:

    f1 k a b c t = (iterate (f0 a b c) t) !! k
    

    然而,在 Haskell 中 type 是一个关键字,所以使用它作为变量名时要小心。

    另外,如果您对 iterate 的工作原理感到好奇,这里有一个实现:

    iterate :: (a -> a) -> a -> [a]
    iterate f x = x : iterate f (f x)
    

    【讨论】:

      猜你喜欢
      • 2010-10-13
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2021-01-12
      • 2011-02-14
      • 2019-02-28
      • 1970-01-01
      相关资源
      最近更新 更多