The problem with "union" and "intersection" is that they operate on
sets, and J has several different good ways of representing sets,
including:

(*) As a sequence of [unique] values
(*) As a bit vector (against a sequence representing the reference universe)

This leads to the following different ways of implementing union and
intersection:

NB. sequence of values, variant 1 (lazy)
union=: ,
intersection=: [ -. -.

NB. sequence of values, variant 2 (compact)
union=: ~.@,
intersection=: [ -. -.

NB. sequence of values variant 3 (normalized)
union=: /:~@~.@,
intersection=: [ -. -.

NB. bit vector variant 1 (formal)
union=: +.
intersection=: *.

NB. bit vector variant 2 (pragmatic)
union=: +.
intersection=: *

Meanwhile, these differences sometimes matter. But, also, hiding them
would have lead to other problems.

I hope this helps?

Thanks,

-- 
Raul



On Tue, Jun 6, 2017 at 4:09 AM, 'Mike Day' via Programming
<[email protected]> wrote:
> FWIW - not much, here, as it isn't relevant to factorial, and on
> investigation, not
> something used in Mr Rice's LISP example,  but,  as Raul's posted a verb for
> "union",
> I've sometimes wondered why J doesn't include primitives for union and
> intersection.
> As I sometimes need intersection,  here's what I use:
>
>     ix =: [-.-.      NB. looks like an emoji!!!
> as in
>     1 2 3 4 5   ix   1 3 5 7 9
> 1 3 5
>
>
> It's probably listed in a phrase or idiom list somewhere,  but never mind.
>
>
> The other thing I meant to say and nearly forgot:
>
> if I were developing a solver for solitaire,  I'd include a variable as a
> parameter for
>
> the size of problem,  eg the number of rows,  1 2 3 etc,  or the ravel-size,
> eg 1 3 6 etc.
>
>
> Thanks,
>
>
> Mike
>
>
>
> On 06/06/2017 00:17, Raul Miller wrote:
>>
>> You could use -. for set difference, and ~.@, for union. You'd
>> probably want to use variables for * though.
>>
>> Or, you could do
>>
>> flip=: -. , -.~
>> legal_moves=: [  #~ 6 = 2 #. e.
>>
>>     1 2 3 4 5 (-.,-.~) 4 5 6
>> 1 2 3 6
>>
>>     move_table ([ #~ 6 = 2 #. e.) 1 2 3 4 5
>> 1 3 6
>> 2 4 7
>> 2 5 9
>> 1 4 8
>> 3 1 0
>> 5 2 0
>>
>> And, I guess you'd also need to modify search (I'll show the shuffling
>> variant, here):
>>
>> search=:3 :0
>>   ((i.15) flip y) search i.0 3
>> :
>>    NB. x: board; y: moves
>>    if. 1=#x do. y return. end.
>>    for_move. ({~ ?~@#) move_table legal_moves x do.
>>      r=.(x flip move) search move,y
>>      if. #r do. r return. end.
>>    end.
>>    i.0 3
>> )
>>
>> So, ... yes, that would work.
>>
>> Thanks,
>>
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to