1
$\begingroup$

so currently I have my own definition of multiplication between elements but it can only multiply two elements.

MyMult[a_,b_]:=...

I want to multiply all elements in a list together with it i.e.

L = {a,b,c,d}
Ans = MyMult[MyMult[MyMult[a,b],c],d]

This however is messy and I want to create a function that takes in an arbitrarily sized list and outputs this product with MyMult.

Any thoughts on a neat/efficient way of doing it? Preferably one-liner sort of deal so I can define a function with it.

$\endgroup$
  • 1
    $\begingroup$ Perhaps Fold might help? $\endgroup$ – Anne Dec 7 '17 at 4:02
  • $\begingroup$ Fold[myMult, L]? $\endgroup$ – kglr Dec 7 '17 at 4:04
  • $\begingroup$ Thank you so much! $\endgroup$ – Aakash Lakshmanan Dec 7 '17 at 4:20
  • $\begingroup$ SetAttributes[MyMult, Flat] might help. $\endgroup$ – Αλέξανδρος Ζεγγ Dec 7 '17 at 4:51
1
$\begingroup$
Fold[myMult, {a, b, c, d}]

myMult[myMult[myMult[a, b], c], d]

$\endgroup$

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