kill(all)$

maxima : chemical equation ,string display version, 'product reaction,function ',version 1.0,part 2
peter.vlasschaert@gmail.com,06/02/2021

ref:
maxima : chemical equation ,string display version, 'product reaction ',version 1.0a,part 1

load :packages ,option.
==================

load(lrats)$ ratprint:false $  load(stringproc)$

display : chemical component of a reaction.
-----------------------------------------------------------

given:Sodium thiosulfate molecule
===========================

→  given :

p1:[Na,2,S,2,O,3];

→ firstline : first argument disp.
========================

firstdisp(p11):=block([p22:[]],
for i:1 thru length(p11) do(
 if oddp(i) then     
p22:endcons(p11[i],p22)
 else
p22:endcons(ascii(32),p22)    
),
simplode(p22) /* simplode(p22) or return(smplode(p22))into block */
)$

pa:firstdisp(p1);

→secondline : second  argument disp.
=============================

1e)  step :

p3a:charlist(simplode(p1));

2e) step :truth table

p3b:makelist(alphacharp(p3a[k]),k,1,length(p3a));

→  init

p3b1:makelist(0,k,1,length(p3a));

p3b2:makelist(if p3b[k]=true then p3b1[k]:1 
       else p3b1[k]:k,k,1,length(p3a));

→  construct

p3b3:delete(1,p3b2);

→  sum ( = 1 or 2)

pos:makelist(0,i,1,length(p1)/2);

pos[1]:sum(p3b2[i],i,1,p3b3[1]-1)$
for k:2 thru length(pos) do(
pos[k]:sum(p3b2[i],i,p3b3[k-1]+1,p3b3[k]-1)
)$

p3b4:pos;

→   position , indices

npos:[];

for i:1 thru 3 do(
if pos[i] = 1 then 
    npos:endcons([ascii(32),ascii(32),p1[2*i]],npos),
if pos[i] = 2 then 
    npos:endcons([ascii(32),ascii(32),ascii(32),p1[2*i]],npos)    
);

p3b5:npos;

p3b6:simplode(flatten(p3b5));

p3b7:stringp(p3b6);

rem : ascii(32) = space ,ascii(9) = tab, see below :xx=32 or 9
rem : ascii & unicode ,see part 1, 'see above'

seconddisp(p11,xx):= block(
[npos:[]],
 /* 1e:step */
p44:charlist(simplode(p11)), 
/* 2e) step :truth table */        
p55:makelist(alphacharp(p44[k]),k,1,length(p44)),
p55a:makelist(0,k,1,length(p44)),
p55b:makelist(if p55[k]=true then p55a[k]:1 
      else p55a[k]:k,k,1,length(p44)),
/* 3e) step :construct */
p66:delete(1,p55b),
/* 4e) step : sum ( = 1 or 2)*/
p77:makelist(0,i,1,length(p11)/2),
p77[1]:sum(p55b[i],i,1,p66[1]-1),
for k:2 thru length(p11)/2 do(
p77[k]:sum(p55b[i],i,p66[k-1]+1,p66[k]-1)
                            ),
p77a:p77,
/* 5e) step position , indices */
for i:1 thru length(p11)/2 do(
if p77a[i] = 1 then 
    npos:endcons([ascii(xx),ascii(xx),p11[2*i]],npos),
if p77a[i] = 2 then 
    npos:endcons([ascii(xx),ascii(xx),ascii(xx),p11[2*i]],npos)    
                  ),
p88a:npos,
p88b:simplode(flatten(p88a)),
/* 6e) update we need removes , ones in the string 5e) step */
p99:ssubst(ascii(32),"1",p88b)
)$

rem : seconddisp: ' only init list =>   npos :[]
rem  :seconddisp: replace "1" by space = ascii(32)

first example :Na2S2O3: Sodium thiosulfate molecule
=========================================
DUTCH :natriumthiosulfaat
wikipedia :https://en.wikipedia.org/wiki/Sodium_thiosulfate

pb:seconddisp(p1,32);

disp(pa,pb)$ /* pa: see above */

second example : HNO3 :Nitric acid
===========================================
DUTCH : salpeterzuur ,othernames IUPAC names.(see google)
wikipedia :https://en.wikipedia.org/wiki/Nitric_acid

other example: HNO
                                    3

p1e:[H,1,N,1,O,3];

pa1:firstdisp(p1e);
pb1:seconddisp(p1e,32);

disp(pa1,pb1)$

---------------------------------------------------------------------------------------------------------------

How to remove indices  {H, N }because they are ones : see 'seconddisp function'

----------------------------------------------------------------------------------------------------------------

future :[K,4,Fe,1,CN,,6], only here CN  = carbon & nitrogen (combine molecule)

third example : K4Fe(CN)6:Potassium ferrocyanide
=============================================
DUTCH:Kaliumhexacyanoferraat(II)
wikipedia:https://en.wikipedia.org/wiki/Potassium_ferrocyanide

general way: coupled ,see CN  ( complex : structure, wikipedia )
-----------------

p2e:[K,4,Fe,1,CN,6];    /* general way : to display the molecule */

pa2:firstdisp(p2e);
pb2:seconddisp(p2e,32);

disp(pa2,pb2)$

rem :coupled : (CN)6  = CN,6

chemical way :
--------------------

p2ea:[K,4,Fe,1,C,6,N,6]; /* chemical way : to display the molecule */

pa3:firstdisp(p2ea);
pb3:seconddisp(p2ea,32);

disp(pa3,pb3)$

   =>   https://nl.webqc.org/molecular-weight-of-K4Fe%28CN%296.html