Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

newbie here.

I want to define a function that takes a ragged array and return another one, but how.

Say, I want to define a simple function behaves like this {var,itr} := {var,itr+1}. var can be anything, while itr is an integer.

First, I know, this will work. fun[{var_,itr_}] := {var,itr+1}

But how to use Function to do this, because I want to generate a function that takes a ragged array.

I tried this without a second thought. gen[d_]:=Function[{{var,itr}},{var,itr+d}] Which had failed, of course.

It seems Function could only use a symbol or a list of symbol as its first parameter.


The reason is, I'm trying to get the iteration times of NestWhile[f, expr, test], it seems f could only takes 1 parameter. So I need a function that will take a ragged array to do this.

But I don't want to write such function every time, so I want to define a function that could generate one for me.

Completely example

Oringinally. NestWhile[#+1&, 1, #<100&] What I want to do is below. NestWhile[gen[#+1&],{1,0},#<100&] And gen is a function that generates a such function. Below is what I do. gen[fun_]:=Function[{var,itr},{fun[var],itr+1}] That's where I'm wrong.

share|improve this question

closed as unclear what you're asking by bobthechemist, Oleksandr R., ciao, Michael E2, m_goldberg Apr 18 '14 at 2:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

I read this question but I did not understand it. What exactly do you mean by "ragged list?" I am familiar with the term ragged array, but you do not seem to mean that. Would you please give a complete example of the exact input and output that you want from your function? –  Mr.Wizard Apr 17 '14 at 7:00
I don't understand "ragged list" either, but what you're trying to do might be achieved using this way: f[{x_,y_}]:=[{x,y+#}]&, which returns a pure function, which you can invoke by passing the value corresponding to your d parameter. For example: f[{4, 0}][4] would give you {4, 4}. (OK I missed part where you said you wanted to do this using Function, but I'll leave this comment here for a while anyway.) –  Aky Apr 17 '14 at 7:25
Concur - please provide complete examples. In addition, you can't use Function in the way you've posted: the first arugment must be a symbol or list of symbols. –  ciao Apr 17 '14 at 7:27
@Mr.Wizard Sorry for using wrong term, I mean something like this {{1,2,3},1}. I guess that's "ragged array". I'll change it soon. –  Ply_py Apr 17 '14 at 7:32
No, that's not a ragged array either, at least not in the typical usage. (Usually it refers to an array with rows or columns of unequal length, e.g. {{1}, {2, 3}, {4, 5, 6}}.) I'm pretty sure this question is going to turn out to be a duplicate of Mathematica Destructuring or possibly Is there any way to define pure functions with optional arguments? Please take a look at those Q&A's and let me know if your question is answered. –  Mr.Wizard Apr 17 '14 at 7:48

1 Answer 1

up vote 0 down vote accepted


Maybe its help you

f = Function[{mas}, {mas[[1]], mas[[2]] + 1}];


f[{{1, 2}, {3, 4}}]

{{1, 2}, {4, 5}}

share|improve this answer
This is not what I want, there should be only one parameter, which is the ragged array. –  Ply_py Apr 17 '14 at 7:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.