Following discussion in thread Peculiarity of Link I revised my
J approach to sets.  Here are examples.  Definitions are below.

    ] S =: set 4;2;3;2;1
+-+-+-+-++
|1|2|3|4||
+-+-+-+-++
    ] T =: set 3;4;5;6
+-+-+-+-++
|3|4|5|6||
+-+-+-+-++
    S n T   NB. intersection
+-+-++
|3|4||
+-+-++
    E is (S n T) less S   NB. empty set
1
    1 get S n T
4
    2 get S n T
|index error: get
|   2     get S n T

    NB. Puzzle: what is the element of
    ]A =: set a:
+++
|||
+++
    NB. ?

Here are the definitions.

NB. Proposal -- A "set" is a list of boxes, without duplicates,
NB. with tail <i.0 0 and curtail sorted.  You can think of the
NB. tail as a "set marker" that permits the empty set to be a
NB. non-empty list.  Sorting the curtail makes it easer to test
NB. whether a set is an element of another set and whether two
NB. sets have the same elements and so are regarded as the same
NB. set.

isset =: [: *./ (1 = #...@$),(0 < L.),(-: ~.),(<@empty -: {:),(-: /:~)@}:

NB. empty =: (i.0 0)"_  is standard in z locale, from stdlib.ijs

set =: <@empty ,~ [: /:~...@~. ] -. <@empty

NB. usage set box or set list-of-boxes creates a set

is =: -:               NB. usage set is set

NB. An "element" is the array contained in a set box
NB. except that i.0 0 is not an element.

iselement =: (<@[ e. ])*.([: -. empty -: [)  NB. array iselement set

in =: iselement        NB. array in set

count =: <:@#          NB. count set gives number of elements

index =: }:@[ i. <@]   NB. set index array

get =: [:`({:: }:)@.(1 > l...@[)  NB. index get set

u =: [: set -. , ]     NB. union

less =: -. , <@empty   NB. difference

n =: [ less less       NB. intersection

subset =: [ is n       NB. set subset set

]E =: , < i.0 0         NB. empty set

++
++

]V =: set 1 ; (set (set 3;4);8;<(set 8;9) ) ; 10  NB. Example of Butch Lakeshore

+-+--+------------------++
|1|10|+-+------+------++||
| |  ||8|+-+-++|+-+-++||||
| |  || ||3|4||||8|9||||||
| |  || |+-+-++|+-+-++||||
| |  |+-+------+------++||
+-+--+------------------++

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to