Is there a reason for `iterate` to start with original element?

时间:2016-08-31 18:15:35

标签: haskell

I sometimes use iterate but I rarely need the argument in the resulting list.

From the documentation:

iterate f x == [x, f x, f (f x), ...]

Although I can drop 1 it, I wonder whether there exists a good reason (maybe a free theorem) independent from implementation for designing basic functions like this one.

2 个答案:

答案 0 :(得分:15)

When talking about iterated function application, it is convenient to define f^0 == id, so that identities like f^m(f^n(x)) == f^(m+n)(x) hold for all natural numbers m and n.

With that definition in mind, iterate satisfies the following identity by including the argument first:

iterate f x !! n == f^n x

答案 1 :(得分:1)

如果您的输出只有iterate f x,则可以轻松获得所需的内容。但是,如果您只有[f x, f (f x),...],那么您无法获得iterate f x,您需要了解更多信息 - 例如,了解原始x。因此,将x放在前面意味着没有充分理由丢失信息。