MathGroup Archive 2010

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

Search the Archive

Re: Nest and Fold don't respect HoldFirst?

  • To: mathgroup at
  • Subject: [mg109273] Re: Nest and Fold don't respect HoldFirst?
  • From: Rui <rui.rojo at>
  • Date: Tue, 20 Apr 2010 05:50:11 -0400 (EDT)
  • References: <hqc0vu$ftg$>

On Apr 17, 7:02 am, ap < at> wrote:
> Dear group,
> HoldFirst attribute set that allows it to modify the global variable
> (parameter) passed to it.
> returns the modified list, as follows:
> SetAttributes[f,HoldFirst]
> Then if I simply do
> mylist=Range[10]
> I get
> f[mylist]
> as expected.
> Nest[f, mylist, 3]
> I get
> symbol. >>
> assignment of the random integer fails. The same behaviour results
> variables.
> as possible. I have an alternative implementation where instead of
> simple Do loop to modify the list.

I think this is my first answer in the group. This is my humble
opinion on this matter:

* Not sure functional tend to lead to efficient.
* Yeah, I agree, Nest seems to not respect the HoldFirst.

fHF[_fHF] := "Hold"; fHF[_] := "NoHold"; f[_f] := "Hold"; f[_] :=
SetAttributes[fHF, HoldFirst];
Now if I do
In: {f[f[f[0]]], fHF[fHF[fHF[0]]]}
Out: {"NoHold", "Hold"}
In: Composition[fHF, fHF, fHF][0]
Out: Hold
In: Nest[fHF, 0, 3]
Out: NoHold

* In your case, f[f[MyList]] doesn't work either, so you're not yet in
the point of blaming Nest.
In the case of  f[f[MyList]]:
What Mathematica does is:
- Evaluates the outermost "f" to "f", nothing changes
- Sees the HoldFirst, so doesn't evaluate f[MyList] just like you told
it to
- Replaces var in your definition by f[MyList] and, among other
things, tries to do f[MyList][[2]]= something, which is an error and
not what you want.

*The best way that I've read so far is doing it like Bob's f2
f2[var_] := ReplacePart[var,
   2 -> RandomInteger[{0, Length[var]}]];

This looks like "pass by value", but I think it's not, in the sense
that var is just an alias of the list, so when you call f[f[...]] you
apply ReplacePart to the same piece of data without making copies. So
overall I'm guessing it's quite efficient. However, I guess that in
the last assignment  MyList=Nest[f, 3, MyList], it's probably making
all the useless assignments instead of just the second element, but I
doubt that makes a difference compared to other things I've read. For
example, I'm not confident on the efficiency of your way of returning
var by creating a list and extracting the second element. Create +
extract seem worse than just using CompoudExpresion ( ; ) Meaning:
f[var_] := (var[[2]] = RandomInteger[ {0,Length[var]} ];   var)
So IIIi don't think Bill is right when he says you're making local
The MapAt choice I think it will take you ages.
Bob's f does however create a local variable, so probably takes a
little longer too.

  • Prev by Date: Re: ArrayPlot coordinates scaling for overlays
  • Next by Date: Re: ArrayPlot coordinates scaling for overlays
  • Next by thread: No state-space form possible example