Message Boards Message Boards

0
|
4527 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

AppendTo vs Reap Sow

Posted 9 years ago

I am a little confused with this two functions: AppendTo adds and resets the variable while Reap and Sow have the attribute Hold and do not return unless finished:

t = Flatten[Reap[For[i = 1, i < 40, i++, Drop[t,{3}]; Print[t]; Sow[i]]][[2]]] 

This will print just t 40 times, not iteratively.

How can I construct a list so that it would update variable in a process of creation as some operations needs to be performed on it meanwhile? This is straightforward with AppendTo but not so with Reap or anything else.

Thanks.

POSTED BY: Al Guy
2 Replies
Posted 9 years ago

Not sure NestWhile will work in my case: It generates random number and then sees whether there is a matching pattern in a nested list, if yes then replaces that element with it and does sub algebra of merging sublists, if not the just appends it to the list.

POSTED BY: Al Guy
Posted 9 years ago

It is perhaps difficult to decide what to suggest without knowing what kind of update you are going to do.

Perhaps you could adapt something like this

t = Range[20];
f[t_] := (Print[Drop[t, {3}]]; Drop[t, {3}]);
NestWhile[f, t, Length[#] > 6 &]

but there is a lot to understand about what is going on inside that.

There are an assortment of other iterative functions available, but almost all those are very different from the procedural way of thinking.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract