Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11089 > unrolled thread

Putting a Test or Condition on the Right-Hand Side of a Function

Started byGregory Lypny <gregory.lypny@videotron.ca>
First post2012-07-26 07:38 +0000
Last post2012-07-27 08:59 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Putting a Test or Condition on the Right-Hand Side of a Function Gregory Lypny <gregory.lypny@videotron.ca> - 2012-07-26 07:38 +0000
    Re: Putting a Test or Condition on the Right-Hand Side of a Function Sébastien Roy <roys3d@gmail.com> - 2012-07-27 08:59 +0000

#11089 — Putting a Test or Condition on the Right-Hand Side of a Function

FromGregory Lypny <gregory.lypny@videotron.ca>
Date2012-07-26 07:38 +0000
SubjectPutting a Test or Condition on the Right-Hand Side of a Function
Message-ID<juqs5s$67q$1@smc.vnet.net>
Hello everyone,

I have a simple function that displays the integer part of a real number if the fraction part is less than a millionth.

	dropDecimal[x_] := If[FractionalPart[x] < .000001, IntegerPart[x], x]

In trying to learn more about functional programming, I was wondering whether there is a way to put the condition or test on the right-hand side of the 
function.

Gregory

[toc] | [next] | [standalone]


#11090

FromSébastien Roy <roys3d@gmail.com>
Date2012-07-27 08:59 +0000
Message-ID<jutl9g$cii$1@smc.vnet.net>
In reply to#11089
Le jeudi 26 juillet 2012 03:38:36 UTC-4, Gregory Lypny a =E9crit :
> I have a simple function that displays the integer part of a real number if the fraction part is less than a millionth.
>
> 	dropDecimal[x_] := If[FractionalPart[x] &lt; .000001, IntegerPart[x], x]
>
> In trying to learn more about functional programming, I was wondering whether there is a way to put the condition or test on the right-hand side of the
> function.

You could define it this way:

drop[x_] := IntegerPart[x] /; FractionalPart[x] < 0.000001;
drop[x_] := x;

The first definition applies only if the test is true. The second "more general" definition will apply if no "less general" definition is applicable.

Sebastien

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web