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.

There are functions like Join[ ] that accept a variable number of arguments. Join is a flat function, as the order of arguments matters, but Join[a,b,c] equals Join[a,Join[a,c]]. I want to create a module that has the same behavior.

It's no problem to create a function with two variables. In this example nested lists are joined horizontally:

JoinH[f_,g_] := Transpose[Join[Transpose[f],Transpose[g]]];

How do I rewrite this function/module so that I can pass a variable number of arguments to this function, like JoinH[a,b,c]?

share|improve this question
1  
You mean something like adding the definition JoinH[a_, b_, rest___] := joinH[a, JoinH[b, rest]]? –  jVincent May 17 '13 at 12:48
    
Yes, but is it possible to write this in one line/function/ in a closed form without having two declarations of the same function? –  kromuchi May 17 '13 at 13:01
    
Can you change the Attributes ? –  b.gatessucks May 17 '13 at 13:10
    
I guess you found my answer useful. I'm glad I could help, and thanks for the Accept. However, waiting longer may encourage additional answers that bring a different perspective. –  Mr.Wizard May 17 '13 at 13:38
    
Alright, let's wait then ;) –  kromuchi May 17 '13 at 15:36

1 Answer 1

First, I'd like to point out that your "JoinH" function is already implemented by Join:

a = {{1, 2}, {3, 4}};
Join[a, a + 5, 2]
{{1, 2, 6, 7}, {3, 4, 8, 9}}

Second, you don't need Flat or whatever if you write the function to natively handle multiple arguments:

jh[m__] := Transpose[Join @@ Transpose /@ {m}]

jh[a, jh[a + 5, a + 11]]
jh[a, a + 5, a + 11]
{{1, 2, 6, 7, 12, 13}, {3, 4, 8, 9, 14, 15}}
{{1, 2, 6, 7, 12, 13}, {3, 4, 8, 9, 14, 15}}

Third, you should also look at the documentation for Fold.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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