3

I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}.

First solution

{3, 5/2, 1 } /.  Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]}  
(*{3, {2, 3}, 1}*)

works fine. Second try

{3, 5/2, 1} /. (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}  )
(*{{3, 2, 1}, {3, 3, 1}}*)

fails.

What's wrong with my second version? Thanks!

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55

1 Answers1

3

Replace with level specification 1 gives the expected result:

Replace[{3, 5/2, 1}, (in_ /; (! IntegerQ[in]) :> {Floor[in], Ceiling[in]}), 1]

{3, {2, 3}, 1}

With ReplaceAll the pattern in_ /; (! IntegerQ[in]) matches {3, 5/2, 1} and it is applied to it to give {Floor[{3, 5/2, 1}], Ceiling[{3, 5/2, 1}]}.

kglr
  • 394,356
  • 18
  • 477
  • 896