Subject: How would you do it in COSY
From: Bob Armstrong <[email-masked]>
To: [email-masked]
Date: Sun, 09 Jul 2023 13:19:31 -0600

Conor , 

The em below from Avi Gross ,  a fellow very long term investor in a `
smart glass company _in development_ , ie: negligible income , about as
long as _CoSy_ [1] ,  triggered my following the link [2] Avi mentioned
, eventually to your APL vs BQN vs J vs Q vs NumPy vs Julia vs R [3] .

While _CoSy_ [1] is still only my creation , the product of an adult
life in APL [4] , evolved and simplified thru K [5] , as an open
vocabulary in Forth , it is at the same level of expressive power as the
languages you demonstrate but _far_ simpler being Forth RPN -- and
_radically_ more flexible being open code all the way to the ` silicon .
Chuck Moore's simple use of _whitespace_ as the prime delimiter is an
unappreciated brilliance .

I'll give a rather complete answer to Avi's question which in the link
is :

    Find the GCD (greatest common divisor) of the smallest and largest
numbers in an array

and the solution given in Dyalog APL is

    ⌈/∨⌊/

I believe ` or , ∨ , was extended to also be gcd after I split with APL
, following Arthur Whitney into K because of its greater simplicity and
transparency of structure . Thus _CoSy_ [1] is purely _lists of lists_ .
In fact at the Forth ( hardware ) level the 3 cell header of an object
is simply `( Type Count refCount )` . Thus it intrinsically follows your
" Leading Axis " notion .

I wasn't sure whether I had implemented gcd in 4th._CoSy_ [1] so I
checked old K._CoSy_ [1] . This is the definition I used there :
gcd
 Greatest Common Divisor . From Eugene McDonnell's Kidioms list 
{ * | 1 + & &/' 0 = x !/: 1 + ! &/ x }
It works on whole lists . When I examine it , it's not terribly
efficient . 

I see that I do have a gcd in the current
https://cosy.com/4thCoSy/Code/CoSy/math.f script : 
: _gcd ( a b -- c) | Jack Browns recursive  greatest common divisor |
raw Forth 
    dup if swap over mod _gcd else drop then ;
 | From http://ronware.org/reva/wiki/index.php/Intermediate_Tutorial

: gcd ' >_ on2 _gcd _i ;     | ' gcd at CoSy level 
Brown's algorithm looks pretty efficient . I don't think the recursion
could ever go very deep . Here are the definitions and  the x86 for mod
and /mod which it calls .
   `( mod /mod )` { dup Help $ See ,L } 'm
( 
 ( 
  mod ( a b -- n ) 'forth
Context: ~
Source in: src/reva.f
    Divide a by b, and put the modulus (remainder) on TOS: n=a%b  

See also: * */ + - / /mod 1+ 1- 2* 2/ << >> 

0061BEE8  E8 E3 FF FF FF              call /mod
0061BEED  8B 06                       mov eax,[esi]
0061BEEF  8D 76 04                    lea esi,[esi+04]
0061BEF2  C3                          ret

 ) ( 
  /mod ( a b -- rem quo ) 'forth
Context: ~
Source in: src/reva.f
    Divide "a" by "b", place remainder and quotient on stack 

See also: * */ + - / 1+ 1- 2* 2/ << >> mod 

0061BED0  89 C3                       mov ebx,eax
0061BED2  8B 06                       mov eax,[esi]
0061BED4  99                          cdq
0061BED5  F7 FB                       idiv ebx
0061BED7  89 16                       mov [esi],edx
0061BED9  C3                          ret

 ) )
Incidentally , one of _CoSy_ [1]'s brilliances is that all indexing is
modulo subsuming notions such as _scalar extension_ and eliminating all
length errors . 

Another brilliance of Forth is that the single quote , ' , returns the
address of the next word , even if its a _verb_ . Thus defining verbs
which take both _nouns_ and _verbs _as arguments , I like Iverson &
Hui's use of _adverb_ , is trivial . For instance , the phrase in ' gcd
above 
' >_ on2  applies  >_ 
: >_ ( disclose  non-nested  and free if 0 refs ) dup 0 i@ swap ref0del
; 
to each of the top 2 stack items to get the raw numbers the Forth level
_gcd needs .

Anyway , here's an answer to the original problem
  i( 18 24 32 0 )i >t0

  t0  .+   ' maxi ./ $  ' mini ./  gcd     | dup the list apply ' max
across ,
           | swap ( aka commute ) apply ` min across , then ' gcd
between them 
32 

: minmaxgcd .+   ' maxi ./ $  ' mini ./  gcd ;    | make it a word

  t0 -1 _cut  minmaxgcd    | drop that trailing 0 . Do again .
2 
Conor , I think you can see that _CoSy_ [1] , immature and in need of
more heads as it is , is on a level comparable to those other array
languages , but far simpler -- and being simply a vocabulary in open
Forth , is unmatched in flexibility . _CoSy_ [1] as it stands is just a
starting point -- a bridge between brilliances of Iverson and Moore .

All the best ,

Bob A 

ps ( continuing watching your vid ) | 20230708.0039 | :
Your example of Q , shows it is far more prolix than the K it evolved
from -- after it was clear working with the Whitneys was a dead end and
figuring out how to _roll my own_ APL in Forth was my only path to what
, since I learned of Forth 40 years ago [6] , I saw was possible .

pps :  I really should do the problem in your vid .
: Imat >a i1 i0 cL a@ i1 +i fill a> 2 _take take ;     | function ( verb
) to create Identity matrices

 `i 4 Imat .+ ' reverse 'm ori >t0>     | make the canonical X matrix
( 
 1 0 0 1 
 0 1 1 0 
 0 1 1 0 
 1 0 0 1 
 )

  i( 2 0 0 1 0 3 1 0 0 5 2 0 4 0 0 2 )i i( 4 4 )i take >t1>    | create
data matrix
( 
 2 0 0 1 
 0 3 1 0 
 0 5 2 0 
 4 0 0 2 
 )

  t1 -0+i    | at Forth level : sn ( n -- -1 | 0 | 1 ) dup 0 >I $ 0 <I -
;  | sign of n , signum
( 
 1 0 0 1 
 0 1 1 0 
 0 1 1 0 
 1 0 0 1 
 )

  R0 t0 ==   | R0 always contains last result .
1 

ppps : | 0201 | I strongly recommend anyone watching Conor's not miss
the end [7] , the C++ approach .

| --

-------- Forwarded Message -------- 

 		SUBJECT:
 		How would you do it in COSY

 		DATE:
 		Fri, 7 Jul 2023 10:04:00 -0400

 		FROM:
 		[email-masked]

 		TO:
 		[email-masked]

https://www.r-bloggers.com/2023/07/array-languages-r-vs-apl/ 

 Avi 

| --

Bob A 

Peace thru Freedom 
Honesty enforced thru Transparency , 

-- [email-masked] -- Bob Armstrong Vita [8] -- 719.337.2733 

 		 _CoSy_ [1]  The Simplest Most ` Powerful Computing Language
Environment

_I reserve the right to post all communications I receive or generate to
CoSy website for further reflection _ 

-- 28124 State Highway 67 Woodland Park CO 80863-9711 

Links:
------
[1] https://cosy.com/
[2] https://www.r-bloggers.com/2023/07/array-languages-r-vs-apl/
[3] https://youtu.be/8ynsN4nJxzU
[4] https://www.dyalog.com/
[5] https://kx.com/
[6] https://www.cosy.com/language/cosyhard/cosyhard.htm
[7] https://youtu.be/8ynsN4nJxzU?t=2390
[8] https://www.cosy.com/BobA/vita.htm