# new operators times by divide by plus minus

So, I've had times[k] for quite a while now, but not the rest. And it was implemented in an ugly, and not perfect, way. Anyway, all tidied now. And using the same code we can now also do divide, plus and minus. This means we can now do a few things in a neater way.

First up, factorial.
The old way:
```fact |0> => |1>
n-1 |*> #=> arithmetic(|_self>,|->,|1>)
fact |*> #=> arithmetic( |_self>, |*>, fact n-1 |_self>)
```
The new way:
```fact |0> => |1>
fact |*> #=> arithmetic( |_self>, |*>, fact minus[1] |_self>)
```
Fibonacci the old way:
```fib |0> => |0>
fib |1> => |1>
n-1 |*> #=> arithmetic(|_self>,|->,|1>)
n-2 |*> #=> arithmetic(|_self>,|->,|2>)
fib |*> #=> arithmetic( fib n-1 |_self>, |+>, fib n-2 |_self>)
```
The new way:
```fib |0> => |0>
fib |1> => |1>
fib |*> #=> arithmetic( fib minus[1] |_self>, |+>, fib minus[2] |_self>)
```
And from a long, long time ago we have this aspirational code to implement currency exchange information:
```to-USD |currency: USD: _x> => |currency: USD: _x>
to-USD |currency: GBP: _x > => |currency: USD: _x*1.62>

to-GBP |currency: USD: _x> => | currency: GBP: _x*0.62>
to-GBP |currency: GBP: _x> => | currency: GBP: _x >
```
I say aspirational since I never found a clean way to implement this. I thought I might have had to change the parser and a bunch of other things. Now we can do it simply enough:
```to-USD |currency: USD: *> #=> |_self>
to-USD |currency: GBP: *> #=> merge-labels(|currency: USD: > + times-by[1.62] extract-value |_self>)

to-GBP |currency: USD: *> #=> merge-labels(|currency: GBP: > + divide-by[1.62] extract-value |_self>)
to-GBP |currency: GBP: *> #=> |_self>
```
Again from long ago I had this for temperature conversion:
```to-Kelvin |temperature: Kelvin: _x > => |temperature: Kelvin: _x >
to-Celsius |temperature: Kelvin: _x > => |temperature: Celsius: (_x - 273.15) >
to-Fahrenheit |temperature: Kelvin: _x> => |temperature: Fahrenheit: (_x*9/5 - 459.67) >

to-Kelvin |temperature: Celsius: _x > => |temperature: Kelvin: (_x + 273.15) >
to-Celsius |temperature: Celsius: _x> => |temperature: Celsius: _x >
to-Fahrenheit |temperature: Celsius: _x> => |temperature: Fahrenheit: (_x*9/5 + 32) >

to-Kelvin |temperature: Fahrenheit: _x > => |temperature: Kelvin: (_x + 459.67)*5/9 >
to-Celsius |temperature: Fahrenheit: _x > => |temperature: Celsius: (_x - 32)*5/9 >
to-Fahrenheit |temperature: Fahrenheit: _x > => |temperature: Fahrenheit: _x >
```
Now we can do it like this:
```to-Kelvin |temperature: Kelvin: *> #=> |_self>
to-Celsius |temperature: Kelvin: *> #=> merge-labels(|temperature: Celsius: > + minus[273.15] extract-value |_self>)
to-Fahrenheit |temperature: Kelvin: *> #=> merge-labels(|temperature: Fahrenheit: > + minus[459.67] times-by[9/5] extract-value |_self>)

to-Kelvin |temperature: Celsius: *> #=> merge-labels(|temperature: Kelvin: > + plus[273.15] extract-value |_self>)
to-Celsius |temperature: Celsius: *> #=> |_self>
to-Fahrenheit |temperature: Celsius: *> #=> merge-labels(|temperature: Fahrenheit: > + plus[32] times-by[9/5] extract-value |_self>)

to-Kelvin |temperature: Fahrenheit: *> #=> merge-labels(|temperature: Kelvin: > + times-by[5/9] plus[459.67] extract-value |_self>)
to-Celsius |temperature: Fahrenheit: *> #=> merge-labels(|temperature: Celsius: > + times-by[5/9] minus[32] extract-value |_self>)
to-Fahrenheit |temperature: Fahrenheit: *> #=> |_self>
```
Yeah, a bit ugly I'm afraid. Though when we swap in "|a> _ |b>" for "merge-labels(|a> + |b>)" in the parser, that should make it a bit cleaner.

This is what that would look like:
```to-Kelvin |temperature: Kelvin: *> #=> |_self>
to-Celsius |temperature: Kelvin: *> #=> |temperature: Celsius: > _ minus[273.15] extract-value |_self>
to-Fahrenheit |temperature: Kelvin: *> #=> |temperature: Fahrenheit: > _ minus[459.67] times-by[9/5] extract-value |_self>

to-Kelvin |temperature: Celsius: *> #=> |temperature: Kelvin: > _ plus[273.15] extract-value |_self>
to-Celsius |temperature: Celsius: *> #=> |_self>
to-Fahrenheit |temperature: Celsius: *> #=> |temperature: Fahrenheit: > _ plus[32] times-by[9/5] extract-value |_self>

to-Kelvin |temperature: Fahrenheit: *> #=> |temperature: Kelvin: > _ times-by[5/9] plus[459.67] extract-value |_self>
to-Celsius |temperature: Fahrenheit: *> #=> |temperature: Celsius: > _ times-by[5/9] minus[32] extract-value |_self>
to-Fahrenheit |temperature: Fahrenheit: *> #=> |_self>
```
And if the above looks like too much work. Well, that shouldn't be a problem. It only needs to be written once, and then we can web-load it. eg:
```sa: web-load http://semantic-db.org/sw-examples/gbp-usd-exchange-rate.sw
```
Though another reason why it looks ugly is the explicit data-type. We could leave that off and it would be a bit cleaner. eg:
```to-Kelvin |Kelvin: *> #=> |_self>
to-Celsius |Kelvin: *> #=> |Celsius: > _ minus[273.15] extract-value |_self>
to-Fahrenheit |Kelvin: *> #=> |Fahrenheit: > _ minus[459.67] times-by[9/5] extract-value |_self>

to-Kelvin |Celsius: *> #=> |Kelvin: > _ plus[273.15] extract-value |_self>
to-Celsius |Celsius: *> #=> |_self>
to-Fahrenheit |Celsius: *> #=> |Fahrenheit: > _ plus[32] times-by[9/5] extract-value |_self>

to-Kelvin |Fahrenheit: *> #=> |Kelvin: > _ times-by[5/9] plus[459.67] extract-value |_self>
to-Celsius |Fahrenheit: *> #=> |Celsius: > _ times-by[5/9] minus[32] extract-value |_self>
to-Fahrenheit |Fahrenheit: *> #=> |_self>
```
And we could abbreviate it even more:
```to-K |K: *> #=> |_self>
to-C |K: *> #=> |C: > _ minus[273.15] extract-value |_self>
to-F |K: *> #=> |F: > _ minus[459.67] times-by[9/5] extract-value |_self>

to-K |C: *> #=> |K: > _ plus[273.15] extract-value |_self>
to-C |C: *> #=> |_self>
to-F |C: *> #=> |F: > _ plus[32] times-by[9/5] extract-value |_self>

to-K |F: *> #=> |K: > _ times-by[5/9] plus[459.67] extract-value |_self>
to-C |F: *> #=> |C: > _ times-by[5/9] minus[32] extract-value |_self>
to-F |F: *> #=> |_self>
```
And I think that is enough for today! Though BTW, the above general scheme can be used for conversions of other units. Say distances, weights, etc.

Home
previous: ket arithmetic
next: new feature list 2 sp

updated: 19/12/2016
by Garry Morrison
email: garry -at- semantic-db.org