MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Something in the spirit of letrec

Hi all,

I'm trying to somehow come up with a function WithSelf such that


will result in {a,a+b,a+b+c}. The idea is that the each
item in the argument may depend on the previous ones;
so for example "self[1]+b" means "what's in the first position
in this list, plus b". There will be no "forward references"
in the argument.

I've tried with

  SetAttributes[WithSelf, HoldFirst];
  WithSelf[l_, self_] := Module[{ll, r = {}},
      ll = Function[self, #] & /@ l;
      Scan[AppendTo[r, #[r]] &, ll];

This gives the correct correct results, but emits warnings:
for example,

  WithSelf[{1, self[[1]] + 2, self[[2]] + 3, self[[1]]}, self]

results in {1, 3, 6, 1}, which is OK, but emits a few of

  Part::partd: Part specification self[[1]] is longer than depth of

I guess there is some evaluation going on which I am
not seeing...

I'm quite sure what I am trying to do is possible... but how?


-- m

  • Prev by Date: Converting Mathematics slides into PDF
  • Next by Date: Re: How to treat this false singular point?
  • Previous by thread: Re: Re: Re: Converting Mathematics slides into PDF
  • Next by thread: Re: Something in the spirit of letrec