문제

The following expression shows a truth table for each of the 16 primitive Boolean operations:

   (0 b./; 1 b./; 2 b./; 3 b./; 4 b./; 5 b./; 6 b./; 7 b./; 8 b./; 9 b./; 10 b./; 11 b./; 12 b./; 13 b./; 14 b./; 15 b./) ~ i.2
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0 0│0 0│0 0│0 0│0 1│0 1│0 1│0 1│1 0│1 0│1 0│1 0│1 1│1 1│1 1│1 1│
│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

How can I refactor the part in parentheses to remove the duplication?

clarification:

The goal here isn't to produce the table, but rather to learn how to produce new verbs dynamically. In order to reduce the parenthesized expression, I would want to factor out the symbols ; , / and b. , and then replace the numbers with i.10.

The ; symbol is easy enough:

   ;/i.16
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬──┬──┬──┬──┬──┐
│0│1│2│3│4│5│6│7│8│9│10│11│12│13│14│15│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴──┴──┴──┴──┴──┘

But I'm having a hard time finding a way to produce a new verb from each element in the list.

I think maybe I'm looking for some kind of higher-order combinators that allow using & and @ with something other than constants.

For example, nn leftBondEach v might make an array of verbs equivalent to n0 & v; n1 & v; ... ; nn & v :

   bverbs =: (i.16)(leftBondEach)b. NB. would mean (0 b.; 1 b.; ...; 15 b.)
   0 bverbs 0
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│0│0│0│0│1│1│1│1│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
   0 bverbs 1
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│1│1│1│1│0│0│0│0│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

I think I need something similar in order to append the / to each function.

Then again, this is J and there's probably a completely different way to approach these problems that I just haven't grokked yet. :)

도움이 되었습니까?

해결책

What about:

<"2@|:@( (i.16) b./~) 0 1

    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    |0 0|0 0|0 1|0 1|0 0|0 0|0 1|0 1|1 0|1 0|1 1|1 1|1 0|1 0|1 1|1 1|
    |0 0|0 1|0 0|0 1|1 0|1 1|1 0|1 1|0 0|0 1|0 0|0 1|1 0|1 1|1 0|1 1|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Just noticing that $ (i. 16) b. /~ 0 1 is 2 2 16, and you'd want to have 16 boxes with size 2x2 ...

다른 팁

The most straight forward conversion, I can think of, is:

(3 :'y b./ ~ i.2') each i.16

edit on clarification

1. You can define something like f =: 4 :';/x b. y', so:

(i.16) f 0
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│0│0│0│0│1│1│1│1│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
(i.16) f 1
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│1│1│1│1│0│0│0│0│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

2. You can "apply" ;/ to (i.16) b. with At (@):

f =: ;/@((i.16) b.)
f 0
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│0│0│0│0│1│1│1│1│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
f 1
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│1│1│1│1│0│0│0│0│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

3. if you want leftBondEach to work on any verb, then it's a conjuction:

leftBondEach =: 2 :';/m v y'   
h =: 2 leftBondEach +             NB. equiv. to h =: ;/@(2&+)
h 3 4 5
┌─┬─┬─┐
│5│6│7│
└─┴─┴─┘
h =: 0 leftBondEach ((i.16) b.)
h 0
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│0│0│0│0│1│1│1│1│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
h 1
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│1│1│1│1│0│0│0│0│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

4. etc you can reshape the result like jpjacobs did etc.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top