# How to combine the elements of two lists using a user-specified function

I have two lists of integers. From these lists I want to produce a list where each element is a fraction derived from a corresponding pair of elements in the original lists, the smaller integer of the pair should be in the numerator.

For example, from T1 = {0, 2, 8}, T2 = {3, 5, 6} and I want to produce T ={0, 2/5, 6/8}. But I have no idea how to do it in Mathematica. I will be grateful for any help.

One way:

t1 = {0, 2, 8};
t2 = {3, 5, 6};
If[# > #2, #2/#, #/#2] &,
{t1, t2}
]

A simple numeric approach that is naturally vectorized due to use of Listable functions:

f[a_, b_] := Quiet[ (a/b)^Sign[b - a] ]

Test:

f[ {2, 3, 6, 5, 9, 7, 5}, {5, 4, 3, 4, 9, 3, 1} ]
{2/5, 3/4, 1/2, 4/5, 1, 3/7, 1/5}

Here is a completely different approach based solely on Attributes:

SetAttributes[f2, {Orderless, Listable}]

f2[a_, b_] := a/b

Test:

f2[{3, 1, 7}, {2, 4, 0}]
{2/3, 1/4, 0}
• Very nice, but produces an infinite expression error when 0 is in the blist. Mar 12, 2015 at 8:59
• @SquareOne I forgot about that case. Thanks. Mar 12, 2015 at 9:02
• This new version is very elegant. By the way, your first version produces a correct answer (it does not fail as you said), the infinite expression message was only a warning not an error as I said (same warning also in one of @Karsten solutions). Mar 12, 2015 at 9:16
• @SquareOne Thanks, again. I guess I could just wrap that in Quiet. Mar 12, 2015 at 9:20
Rational @@@ Sort /@ Transpose[{T1, T2}]
(*{0, 2/5, 3/4}*)
Min@#/Max@# & /@ Transpose[{t1, t2}]
{0, 2/5, 3/4}
Inner[Min@{##}/Max@{##} &, t1, t2, List]
{0, 2/5, 3/4}
t2/t1 /. x : (# > 1 & | ComplexInfinity) -> 1/x
{0, 2/5, 3/4}