I would like to send a set of Mathematica expressions (i.e. not simple types that are easy to represent and temporarily store in C) in a List using the MathLink C API. I do not know how many there will be before I compute all of them, but I need their count to be able to call MLPutFunction.
How can I conveniently send a List without needing to know the number of elements beforehand?
One idea I had was to put them on a loopback link while counting them, then transfer the contents of the loopback link at once. But I was hoping for a simpler solution.
List, and then each member is added asSequence[dat,a]where a is the next data point, i.e. you make a nested list. Then you can close it by addingSequence[]as your last point, collapsing the whole structure. Alternatively, you could use a loop-link. I'll write it up, if I get the chance. – rcollyer Jan 31 '13 at 18:36Sequence,which will lead to a quadratic complexity because of the run-time splicing, I would use some inert head and build a linked list,Flatten-ing this at the end. See my comments under Arnoud's answer. – Leonid Shifrin Jan 31 '13 at 19:12Internal`Bag, as shown in Leonid's answer to the question on SO linked by Rolf? – Oleksandr R. Jan 31 '13 at 19:25StuffBagand then unpack them later on. Whether it's acceptably efficient to send each item individually would depend on the application, I suppose. – Oleksandr R. Jan 31 '13 at 19:31