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


 _________________________________________ 
||                                       ||
||   Algorithms in the SAM76 language    ||
||                                       ||
||   Ancelme Roichel -- Karl Nicholas    ||
||                                       ||
 _________________________________________ 


 _________________________________________ 
||                                       ||
||             Introduction              ||
||                                       ||
 _________________________________________ 



In the algorithms described and illustrated
in   this  section,  use  is  made  of  the
procedure  named  "print"  to  display  the
script  of  the functions, expressions, and
other text strings.


It may also be interesting to note that all
of the examples which are bounded by wiggly
lines at top and bottom, and circles at the
left  (simulating  a  fragment of form feed
paper)  are executed and solved dynamically
by  the text formatter - thus ensuring that
the  result shown betwen curly braces truly
the  result  of  executing  the  expression
which precedes them.


The  "print"  function is useful in that it
augments  the  capability of the "vt - view
text" function by prefixing each individual
"text"  displayed  with its name bounded by
angle  brackets;  assuming  that texts have
been created as shown:

	o~~~~~~~~~~~~~~~~~~~~~~~
	o  %dt,a,apple/={}
	o  %dt,b,bottle/={}
	o  %pt,b,t/={}
	o
	~~~~~~~~~~~~~~~~~~~~~~


In the foregoing example the adjacent curly
braces  {} merely signify that the value of
the expression is the null string.


The   use   of   "print"  would  cause  the
following display:

< a >
apple

<b>
bo[1][1]le


The  function  "print"  may be used to list
all of the texts currently in the text area
by invoking it using as its argument an "lt
-  list  text"  expression which has a ", -
comma" as its argument. This list is passed
into  the  "print"  expression,  the  first
argument   of  the  list  being  placed  in
"partition   [1]",   and   all   remaining
arguments   being   placed   in   the
"multi-partition"  [#2].  This continues to
function  recursively  until  the  list  is
exhausted  -  that  is  to  say when a null
string is passed into [1], and the "ii - if
identical" expression is satisfied.


All of the texts, or functions found in the
file  ALGO.SAM are thus listed in the table
which accompanies this description of SAM76
algorithms.  The  number which follows each
"text"  name  is  a  count of the number of
characters in the "text" itself. The reader
should  compare this number with the number
of   keystrokes   required  in  some  other
favorite   language  to  achieve  the  same
result.

 _________________________________________ 
||                                       ||
||     Description of the Algorithms     ||
||                                       ||
 _________________________________________ 



The  simple recursive named "fac" generates
the  factorial of any number which replaces
partition value [1] in its use:

	o~~~~~~~~~~~~~~~~~~
	o  %fac,5/={120}
	o
	~~~~~~~~~~~~~~~~~~~~~~~


The  recursive  function  "exp"  is used to
generate  the  exponential  of  "m"  to the
"nth." power. in use "m" replaces partition
[1] and "n" replaces partition [2]:

	o~~~~~~~~~~~~~~~~~~~~~~~~
	o  %exp,3,7/={2187}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~~


The   recursive   expression   named  "mrn"
extracts the "mth." root of any number "n".
It should be noted that this function makes
use  of  the  expression named "exp", which
generates an exponential:

	o~~~~~~~~~~~~~~~~~~~~~~~~~~
	o  %mrn,2187,7/={3}
	o
	~~~~~~~~~~~~~~~~~~~~~~~


The  recursive function "tensor" is used to
generate   a  list  of  successive  numbers
separated  by  "."; the size of the list is
specified   by  the  integer  number  which
replaces partition [1] in the expression:

	o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	o  %tensor,6/={1.2.3.4.5.6}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The expression "dnt" illustrates the use of
the  "ds  -  duplicate  string" function to
cause   the  execution  of  some  procedure
"proc" the desired number of times which is
passed  into  partition [1] thus: "%dnt,6/=
will repeat procedure named "proc" 6 times.



The  procedure "DTR" is used in conjunction
with  expressions  or  texts  named "rom1",
"rom2,  "d",  "r","u"  and "t" to convert a
decimal   number  into  its  ROMAN  numeral
equivalent:

	o~~~~~~~~~~~~~~~~~~~~~~~~~~~
	o  %DTR,1986/={MCMLXXXVI}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Procedures "loop", "lp1" and "lp2" are used
with  each  other  to  enable  the  user to
specify the starting value and the range of
values through which some procedure will be
executed.  The  first  and  third  line  of
"loop"  are  required  only to preserve and
restore special registers 6 and 7 which are
also  used  by the text formatter which was
used  to  print this article, executing the
examples  on  the fly. For instance one may
wish  to  generate the "DTR" procedure over
the range 105 to 116 as illustrated below:

	o~~~~~~~~~~~~~~~~~~~~~~~~~~~
	o  %loop,DTR,105,116/={
	o   105   CV
	o   106   CVI
	o   107   CVII
	o   108   CVIII
	o   109   CIX
	o   110   CX
	o   111   CXI
	o   112   CXII
	o   113   CXIII
	o   114   CXIV
	o   115   CXV
	o   116   CXVI}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~


"HTR"  is  a simple script provided for the
"hex-freak"   which  making  use  of  "DTR"
provides  the  conversion of hexadecimal to
roman numbers:

	o~~~~~~~~~~~~~~~~~~~
	o  %HTR,FF/={CCLV}
	o
	~~~~~~~~~~~~~~~~~~~


"repeat"  is a generalized recursive script
which   makes  use  of  the  multipartition
feature  of  the  SAM76  language  to sweep
through  a list passing in turn each of its
elements into a procedure such as "proc1".



The "skim" expression, using procedures "i"
and  "k"  skims off the first appearance of
each  different  character  of  the  string
which  is  placed as an argument to it. The
result is returned as the value:

	o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	o  %skim,MISSISSIPPI/={MISP}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


In  his  book  "ALGORITHMS  in SNOBOL4" Jim
Gimpel (also author of CTERP) suggests this
problem  as  a  test of string manipulation
capability  of  a  language.  In SAM76 this
problem  may  be  solved  with 100 symbols,
while  in SNOBOL4 the same problem requires
143  symbols. How many symbols are required
in LISP, PASCAL, FORTH &c.?


Everybody  at one time or another wishes to
calculate  the  value of "pi" to any number
of  places;  the  procedure named "pi" does
just  that, and it only requires the use of
addition,  subtraction, multiplication, and
the  test  "ig"  for "if greater than". The
expression  named "PI" is used to pass into
"pi"  a number, the larger that number, the
more accurately "pi" will be computed - but
more time is required:

	o~~~~~~~~~~~~~~~~~~~~
	o  %PI,10/={324}
	o  %PI,100/={31468}
	o
	~~~~~~~~~~~~~~~~~~~~~~~~~~~


The  concepts  from  which  this  procedure
stems  are attributable to NEWTON, and Jaan
Raamot;  an  early  implementation  of this
concept  was  written  in "basic" by Johnny
Gorman.


Script  "ho"  is  a  very simple expression
which  makes  use  of  the  SAM76  language
capability  of changing the number bases of
both   the   decimal   and  binary  systems
dynamically  to convert hexadecimal numbers
to octal equivalents.

	o~~~~~~~~~~~~~~~~~~~
	o  %ho,FF/={377}
	o
	~~~~~~~~~~~~~~~~~~~~~

The  script  "mult"  was  contributed by C.
Jamieson   of   Canada  as  a  solution  to
introducing  floating  point numbers into a
system  which  provides arbitrary precision
integers only.

o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
o  %mult,105.27,27.132/={2856.18564}
o
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"HANOI"  is  the SAM76 language solution to
the  problem  of moving an arbitrary number
of  rings  between  three  towers with user
defined names:

o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
o  %HANOI,4,here,there,middle/={
o  Move Ring 1 from here to middle
o  Move Ring 2 from here to there
o  Move Ring 1 from middle to there
o  Move Ring 3 from here to middle
o  Move Ring 1 from there to here
o  Move Ring 2 from there to middle
o  Move Ring 1 from here to middle
o  Move Ring 4 from here to there
o  Move Ring 1 from middle to there
o  Move Ring 2 from middle to here
o  Move Ring 1 from there to here
o  Move Ring 3 from middle to there
o  Move Ring 1 from here to middle
o  Move Ring 2 from here to there
o  Move Ring 1 from middle to there}
o
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A  list briefly defining 26 out of a set of
more  than 200 available resident functions
of  the  language  which  are  used  in the
algorithms follows:

%ad,d1,d2/              Add "d1" and "d2"
%crd,name/              Char. Right of Div.
%cxb,x/                 Change "X" Base
%ds,d1,s1/              Duplicate String
%dt,name,text/          Define Text
%dx,d/			Decimal to "X" base
%et,name, ... /         Erase Text
%fc,name/               Fetch Character
%fdc,name,d1/           Fetch "d1" Char.
%fe,name/               Fetch Element
%fts,name,string/       Fetch To Span
%hc,name/               How many Characters
%ig,d1,d2,t,f/          If Greater
%ii,s1,s2,t,f/          If Identical
%mu,d1,d2/              Multiply "d1*d2"
%os,string/             Output String
%ps,d,s,string/         Pad String
%pt,name,s1,.../        Partition String
%rs,string/             Reverse String
%su,d1,d2/              Subtract "d1-d2"
%vt,name/               View Text
%xd,x/                  "X" to Decimal
%zd,r,lt,eq,gt/         "Z" Decrement
%zi,r,lt,eq,gt/         "Z" Increment
%zq,r/                  "Z" Query
%zs,r,d/                "Z" Set Register

 _________________________________________ 
||                                       ||
||              REFERENCES               ||
||                                       ||
 _________________________________________ 


"EXOTIC  LANGUAGE  OF  THE  MONTH - SAM76",
Computer Language Magazine, November 1985

"SAM76  Language  Manual",  SAM76 Inc., Box
257 RR1, Pennington, NJ, 08534, USA

"McGraw  Hill Personal Computer Programming
Encyclopedia"

"Dr. Dobbs collection Volume 3, 1977"

 _________________________________________ 
||                                       ||
||             Editor's Note             ||
||                                       ||
 _________________________________________ 


All  of  the  algorithms described above as
well  as the SAM76 language text editor and
formatter  and  the  SAM76  interpreter are
available  for CPM and PC's in the Computer
Language  bulletin  board.  The  PC version
does  require  that the machine be equipped
with the NEC V20 or V30 microprocessor.


  
 SAM76 LANGUAGE ALGORITHMS     algo.sam
---------------------------------------
        
        
        { print }	51
        %ii,[1],,,!%os,(
        < [1] > )    %crd,[1]//%vt,[1]/%os,
        /%print[#2]///
        
        { fac }	31
        %ig,1,[1],1,!%mu,[1],%fac,%su,[1],1/////
        
        { exp }	31
        %ig,[2],,!%mu,[1],%exp,[1],%su,[2],1////,1/
        
        { mrn }	45
        %ig,%exp,[3],[2]/,[1],`
        !%su,[3],1//,`
        !%mrn,[1],[2],%ad,[3],1////
        
        { tensor }	28
        %ig,[1],1,!%tensor,%su,[1],1//.[1]/,[1]/
        
        { dnt }	14
        %ds,[1],!%proc///
        
        { DTR }	26
        %dt,X,[1]/%rom1/%Y/%et,X,Y,Z/
        
        { rom1 }	35
        %ig,%crd,X/,,!%rom2,%fc,X//%rom1///
        
        { rom2 }	54
        %dt,Z,[1]/%pt,Z%d//%dt,Z,%Z%r///%pt,Y%s//%dt,Y,%Y%t//%Z//
        
        { d }	20
        ,0,1,2,3,4,5,6,7,8,9
        
        { r }	30
        ,,I,II,III,IV,V,VI,VII,VIII,IX
        
        { s }	14
        ,I,V,X,L,C,D,M
        
        { t }	14
        ,X,L,C,D,M,*,$
        
        { loop }	102
        %dt,zs6,%zq,6//%dt,zs7,%zq,7//`
        %zs,6,[2]/%zs,7,%su,[3],[2]//%zi,7/%lp1,[1]/`
        %zs,6,%zs6//%zs,7,%zs7//%et,zs6,zs7/

        
        { lp1 }	36
        %lp2,[1],%zq,6//%zi,6/%zd,7,,,!%lp1,[1]///
        
        
        { lp2 }	19
        !
        /%ps,-4, ,[2]/   %[1],[2]/
        

        { HTR }	11
        %DTR,%xd,[1]//
        
        { repeat }	26
        %ii,[1],,,!%proc1,[1]/%repeat[#2]///
        
        { proc1 }	32
        %os,
        %ps,10, ,[1]/%ps,-5, ,%crd,[1]///
        
        { skim }	21
        %dt,,([1])/%rs,%j//%et,/
        
        { j }	24
        %ig,%crd,/,,!%k,&fc,////
        
        { k }	11
        %pt,,[1]/%j/([1])
        
        { PI }	8
        %pi,[1],[1],[1],[1]/
        
        { pi }	259
        %ig,%ig,%ad,[1],%ad,[3],%ad,[3],1///,0,!%ad,[1],%ad,[3],%ad,[3],
        1////,!%su,0,%ad,[1],%ad,[3],%ad,[3],1//////,%ig,%ad,%su,%su,[1]
        ,[2],/,[2]/,1/,0,!%ad,%su,%su,[1],[2]/,[2]/,1//,!%su,%ad,[2],[2]
        /,%ad,[1],1////,!%ii,[2],0,!%ad,[4],[4]//,!%pi,%ad,%su,%su,[1],[
        2]/,[2]/,1/,%su,[2],1/,[3],%ad,[4],[3]/////,!%pi,%ad,[1],%ad,[3]
        ,%ad,[3],1///,[2],%ad,[3],1/,%ad,[4],[2]////
        
        { ho }	31
        %ad,,%dx,%cxb,16/%xd,[1]/%cxb,8///
        
        { mult }	332
        %dt,n1,%rs,[1]//`
        %dt,n2,%rs,[2]//`
        &pt,n1,./&pt,n2,./`
        %dt,n3,0123456789ABCDEF/`
        %dt,n3,%fdc,n3,%qnb///`
        %dt,n4,%mu,%rs,%n1//,%rs,%n2//,!%rr,[3]////`
        %dt,n5,%rs,%n4///`
        %dt,n5,%hc,%fts,n5,%n3////`
        %dt,n3,%ad,%hc,%fe,n1//,%hc,%fe,n2////`
        %dt,n4,%ig,%n5/,%n3/,!%n4//,!%ps,-%n3/,0,%n4/////`
        %fdc,n4,%su,%n5/,%n3///.%fe,n4/`
        %et,n1,n2,n3,n4,n5/
        
        
        
        { HANOI }	70
        %ii,[1],0,,!%HANOI,%su,[1],1/,[2],[4],[3]/!
        Move Ring [1] from [2] to [3]/%HANOI,%su,[1],1/,[4],[3],[2]///
 