1
$\begingroup$

I have a pure function

f:= #1 * #2 * #3 &;

Which I can invoke in a following way:

f[#,2,3]& /@ {1,2,3}

Which yields following result:

{6, 12, 18}

How could I do something like this:

f[#1,#2,3]& /@ {{1,2,3},{1,2,3}}

such that the return product would look like:

{{3,6,9},{6,12,18},{9,18,27}}

I understand there is a syntax error above. I struggle coming up with a functional solution which would avoide iteration and loops. I'd greatly appreciate any assistance.

Example based on OOP (conceptual):

List<Integer> nums = new ArrayList<Integer>();
for(int i = 1; i < 5; i++){
  for(int m = 1; m < 3; m++){
     product = i * m * 3;
     nums.add(product)
  }
}
System.out.println("Product: " + nums.toString());
$\endgroup$
4
$\begingroup$

As mentioned in the comments, the construct you are probably looking for is Outer (see its documentation page).

You could then define the following function to achieve what you indicated, even without using Map:

Clear[func]
func[list1_List, list2_List, factor_?NumberQ] := factor Outer[Times, list1, list2]
func[{1, 2, 3}, {1, 2, 3}, 3]

(* Out: {{3, 6, 9}, {6, 12, 18}, {9, 18, 27}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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