1. Atrthmetic operators

I was emailed privately but I figured I may as well turn it into a community question

Gary Chike said...

The Euphoria code was garnered from here: https://tutorialspoint.pro/euphoria/euphoria_sequences.htm
It appears the arithmetic operators '+ - ' don't work with sequences in Phix.
I was able to figure out the use of '&' vs '+' for sequences.
But I'm stumped on the '-' and ' * ' operators for sequences and the possible equivalents in Phix.

-- sequence x, y, a, b, c <-- original code declared all sequences at one time 
sequence a 
sequence x = {1, 2, 3} 
sequence y = {10, 20, 30} 
--a = x & y -- initializing sequence <-- original code had 'x + y' which does not work in Phix  
a = x + y -- initializing sequence <-- original code had 'x + y' which does not work in Phix  
puts(1, "Value of a = {") 
for i = 1 to length (a) do 
    printf(1, "%d,", a[i]) 
end for 
puts(1, "}\n") 
sequence b = x - y -- does not work for sequences in Phix 
puts(1, "Value of b = {") 
for i = 1 to length (a) do 
    printf(1, "%d,", b[i]) 
end for 
puts(1, "}\n") 
sequence c = x * 3 -- <-- '*' does not work for sequences in Phix, I could use 'repeat ()' here 
puts(1, "Value of c = {")  
for i = 1 to length(c) do  
    printf(1, "%d,", c[i]) 
end for 
puts(1, "}\n") 

Works fine for me here, I get the exact same output (as Euphoria):

Value of a = {11,22,33,} 
Value of b = {-9,-18,-27,} 
Value of c = {3,6,9,} 

I also get the following messages in the Edita message panel, which might/should help:

C:\Program Files (x86)\Phix\e01.exw:18 
sequence c = x * 3 -- <-- '*' does not work for sequences in Phix, I could us.. 
               ^Warning: sq_mul() assumed 
C:\Program Files (x86)\Phix\e01.exw:12 
sequence b = x - y 
               ^Warning: sq_sub() assumed 
C:\Program Files (x86)\Phix\e01.exw:6 
a = x + y -- initializing sequence <-- original code had 'x + y' which does n.. 
      ^Warning: sq_add() assumed 

I should perhaps note that '+' is ambiguous in JavaScript, performing addition or concatenation, whereas in Phix/Euphoria '+' is addition only, and '&' is unambigously concatenation-only.
Some other languages have an ambigous '*' operator, but again in Phix/Euphoria '*' is always multiplication, and sometimes repeat() matches what '*' in some other languages sometimes does.
Euphoria has infix sequence-ops, whereas Phix wants those function-style calls instead, but occasionally auto-substitutes when the only alternative would be a compilation error.

Relevant manual page: http://phix.x10.mx/docs/html/seqops.htm (especially the drop-down at the end).

For completeness, I'd rewrite that example in Phix as:

sequence x = {1, 2, 3}, 
         y = {10, 20, 30}, 
         a = sq_add(x,y), 
         b = sq_sub(x,y), 
         c = sq_mul(x,3) 
printf(1, "Value of {a,b,c} = %v\n",{{a,b,c}}) 
--outputs: Value of {a,b,c} = {{11,22,33},{-9,-18,-27},{3,6,9}} 
new topic     » topic index » view message » categorize

2. Re: Atrthmetic operators

Thank you for posting the code for me until my account could be activated on the forum. I'm not sure why I initially got an error. But it works for now. And thank you for pointing me to the requisite documentation and for rewriting the code for completeness. Cheers - Gary

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu