1 | initial version |

In your last line, you use `h`

to define `h`

itself.

2 | No.2 Revision |

In your last line, you use `h`

to define `h`

itself.

**EDIT** (given the explanation provided in comments): it is not possible to do such a thing. Indeed, when you write `h=[...]`

, the Python name `h`

is created *after* the object `[...]`

is created.

So, you have to define a recursive function as follows:

```
sage: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

That said, i guess (i might be wrong in your motivation), that you wanted to use a list so that when you compute `h[4]`

, you do not have to do the whole recursion by computing `h[3]`

, `h[2]`

, `h[1]`

, `h[0]`

, which is what `hh`

does. The trick is to use the `@cached_function`

decorator, that ocmputes `h[i]`

only once and memoizes the result for further calls. So, you can do:

```
sage: @cached_function
....: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

3 | No.3 Revision |

In your last line, you use `h`

to define `h`

itself.

**EDIT** (given the explanation provided in comments): it is not possible to do such a thing. Indeed, when you write `h=[...]`

, the Python name `h`

is created *after* the object `[...]`

is created.

So, you have to define a recursive function as follows:

```
sage: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

That said, i guess (i might be wrong in your motivation), that you wanted to use a list so that when you compute `h[4]`

, you do not have to do the whole recursion by computing `h[3]`

, `h[2]`

, `h[1]`

, `h[0]`

, which is what `hh`

does. The trick is to use the `@cached_function`

decorator, that ~~ocmputes ~~computes `h[i]`

only once and memoizes the result for further calls. So, you can do:

```
sage: @cached_function
....: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

4 | No.4 Revision |

In your last line, you use `h`

to define `h`

itself.

**EDIT** (given the explanation provided in comments): it is not possible to do such a thing. Indeed, when you write `h=[...]`

, the Python name `h`

is created *after* the object `[...]`

is created.

So, you have to define a recursive function as follows:

```
sage: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

That said, i guess (i might be wrong in your motivation), that you wanted to use a list so that when you compute `h[4]`

, you do not have to do the whole recursion by computing `h[3]`

, `h[2]`

, `h[1]`

, `h[0]`

~~, ~~ again, which is what `hh`

does. The trick is to use the `@cached_function`

decorator, that computes `h[i]`

only once and memoizes the result for further calls. So, you can do:

```
sage: @cached_function
....: def hh(i):
....: if i<1:
....: return 0
....: else:
....: return hh(i-1) + g[i]
....:
sage: h = [hh(i) for i in range(len(d)-1)]
sage: h
[0, 1, 2, 3, 4, 5, 6, 7]
```

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.