# How to construct a new list made from a function that operates on two lists

I need to take two lists (each list contains the number of moles of a chemical). I have a function calledFraction that takes the each entry of the list and then calculates the mole fraction of Chloroform. Each entry in the new list should be the mole fraction calculated by taking
molesChloroform[[1]]/(molesChloroform[[1]] + molesAcetone[[1]]

Here is what I have tried:

molesChloroform = {0.125, 0.100, 0.0625, 0.0375}
molesAcetone = {0.027, 0.068, 0.0952, 0.136}
moleFraction[molesA_, molesB_] = molesA/(molesA + molesB)
{moleFraction[#1, #2]} & @@@ {molesChloroform[[1 ;;]],
molesAcetone[[1 ;;]]}
Out[23]= {{0.555556}, {0.284211}}


This is not the correct output.

-
Do you get the correct output if you change @@@ to @@? Or, better yet, if you use moleFraction[molesChloroform,molesAcetone ]? – kglr Apr 5 '13 at 5:04
yes I do. Thank you. I don't know why I didn't think your simpler solution wouldn't work. both give the same output. And i have verified it is correct. – olliepower Apr 5 '13 at 5:14

## 2 Answers

A solution to compute the ratio for any two lists of mole values, based on MapThread:

mf[mA_, mB_] := MapThread[#1/(#1 + #2) &, {mA, mB}]

mf[molesChloroform,molesAcetone]

(*{0.822368, 0.595238, 0.396322, 0.216138}*)

-

I changed

{moleFraction[#1, #2]} & @@@ {molesChloroform[[1 ;;]],
molesAcetone[[1 ;;]]}


to

{moleFraction[#1, #2]} & @@ {molesChloroform[[1 ;;]],
molesAcetone[[1 ;;]]}


And got this correct result:

{0.822368, 0.595238, 0.396322, 0.216138}


Also

moleFraction[molesChloroform, molesAcetone]


give the same result.

-
Yes, all sub-operations are Listable therefore the function moleFraction can be used directly. See Case #4 here. – Mr.Wizard Apr 5 '13 at 7:04