Correction (add test of sorted):
NB. Usage IsSet array - tests sorted, nub, list, boxed
IsSet =: [: *./ (-: /:~),(-: ~.),(1 = #...@$),(32 = 3!:0)
S NB. Butch's example
+-+--+---------------+
|1|10|+-+-----+-----+|
| | ||8|+-+-+|+-+-+||
| | || ||3|4|||8|9|||
| | || |+-+-+|+-+-+||
| | |+-+-----+-----+|
+-+--+---------------+
IsSet S
1
IsSet E NB. empty set
1
IsSet 1;3;2;4
0
Repeat of earlier work with Butch's example:
]S =: set 1 ; (set(set 3;4);8;<(set 8;9)) ; 10 NB. for < see next note
+-+--+---------------+
|1|10|+-+-----+-----+|
| | ||8|+-+-+|+-+-+||
| | || ||3|4|||8|9|||
| | || |+-+-+|+-+-+||
| | |+-+-----+-----+|
+-+--+---------------+
The set is given in a standard order that makes it easier to test whether a
set
is an element of another set and whether two sets have the same members
andthus
are considered to be the same set. Now let
]one =: 1
1
]two =: set (set 3;4);8;<(set 8;9) NB. < overcomes irregularity in ;
+-+-----+-----+
|8|+-+-+|+-+-+|
| ||3|4|||8|9||
| |+-+-+|+-+-+|
+-+-----+-----+
]three =: 10
10
and we see that
(set one;two;three) is S
1
count S
3
(one member S) *. (two member S) *. (three member S)
1
((set 3;4) member two) *. (8 member two) *. ((set 8;9) member two)
1
To get 8 from two you can notice
two index 8
0
and do
0 get two
8
To get that 8 from S you can similarly do
0 get (2 get S)
8
which can be abbreviated
(2;0) get S NB. Get element 2 then get element 0 from that
8
You can now figure out how to get the other 8 from S.
Kip Murray wrote:
> Corrections:
>
> NB. Proposal - a "set" is a sorted list of boxes, without repetitions.
>
> set =: ([: /:~ ~.) : [: NB. usage set list-of-boxes creates a set
>
> Improvement:
>
> is =: -: NB. usage set is set
>
> Add:
>
> IsSet =: (-: ~.)*.(1 = #...@$)*.(32 = 3!:0) NB. usage IsSet array
>
>
> Please test these and report errors.
>
>
> Kip Murray wrote:
>> Please try the following, let me know if you find errors.
>>
>> NB. Proposal - a "set" is a sorted list of boxes.
>> NB. An element is the array contained in a box.
>>
>> set =: /:~ NB. usage set list-of-boxes creates a set
>>
>> count =: # NB. usage count set
>>
>> in =: <@[ e. ] NB. usage array in set
>>
>> index =: i. < NB. usage set index array
>>
>> get =: {:: NB. usage index get set
>>
>> is =: =&< NB. usage set is set
>>
>> u =: [: set -. , ] NB. union
>>
>> n =: [ -. -. NB. intersection
>>
>> Less =: -. NB. difference
>>
>> subset =: [ is n NB. usage set subset set
>>
>> member =: in NB. usage array member set
>>
>> E =: set 0$<1 NB. empty set
>>
>>
>> Applying this to Butch's example, I get
>>
>> ]S =: set 1 ; (set(set 3;4);8;<(set 8;9)) ; 10 NB. for < see next note
>> +-+--+---------------+
>> |1|10|+-+-----+-----+|
>> | | ||8|+-+-+|+-+-+||
>> | | || ||3|4|||8|9|||
>> | | || |+-+-+|+-+-+||
>> | | |+-+-----+-----+|
>> +-+--+---------------+
>>
>> The set is given in a standard order that makes it easier to test whether a
>> set
>> is an element of another set and whether two sets have the same members and
>> thus
>> are considered to be the same set. Now let
>>
>> ]one =: 1
>> 1
>>
>> ]two =: set (set 3;4);8;<(set 8;9) NB. < overcomes irregularity in ;
>> +-+-----+-----+
>> |8|+-+-+|+-+-+|
>> | ||3|4|||8|9||
>> | |+-+-+|+-+-+|
>> +-+-----+-----+
>>
>> ]three =: 10
>> 10
>>
>> and we see that
>>
>> (set one;two;three) is S
>> 1
>> count S
>> 3
>> (one member S) *. (two member S) *. (three member S)
>> 1
>> ((set 3;4) member two) *. (8 member two) *. ((set 8;9) member two)
>> 1
>>
>> To get 8 from two you can notice
>>
>> two index 8
>> 0
>>
>> and do
>>
>> 0 get two
>> 8
>>
>> To get that 8 from S you can similarly do
>>
>> 0 get (2 get S)
>> 8
>>
>> which can be abbreviated
>>
>> (2;0) get S NB. Get element 2 then get element 0 from that
>> 8
>>
>> You can now figure out how to get the other 8 from S.
>>
>> Puzzle:
>>
>> two member S
>> 1
>> two subset S
>> 0
>> (set <two) subset S
>> 1
>>
>>
>> [email protected] wrote:
>>> ----- Original Message Follows -----
>>> From: [email protected]
>>> To: Programming forum <[email protected]>
>>> Subject: Re: [Jprogramming] sets within a set
>>> Date: Thu, 16 Jul 2009 16:19:23 -0800
>>>
>>>> I wanted a set made up from:
>>>>
>>>> 1. member 1
>>>> 2. set { { 3, 4}, 8, { 8, 9}}
>>>> 3. member 10
>>>>
>>>> So, I believe I am creating my desired set correctly.
>>>>
>>>> I am struggling with howw to extract the 8 from the middle
>>>> of the "2. set {{3, 4}, 8, {8,9}}'
>>>>
>>>> Given testSet=:1; ((3,4); 8; (8,9)); 10
>>>>
>>>> I would have thought it was 1 } 1 } testSet, but is not and
>>>> I get an index
>>>> error. I do a '# 1 } testSet' and it returns 1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm