I have a verbose solution as below, hopefully to show that I.e. Verb family
could be used to matching patterns as from a beginner's view:
society =. (1 ,. 5 7); (2 ,. _.);(3 ,. _.); (4 ,. 1 2); (5 ,. 2 7);(6 ,. 2);(7
,. 8) ;(8 ,. 5)
unique =. {./.~
find =. [: I. e.
p =. |:;society
matches =. ({:p)&find each (unique {.p) NB. indices.
matches=. matches{ L:0 {.p
newFinds =. (<"0 (unique {.p)) ,.L:0 matches
]symSoc =. society , L:0 newFinds
-----Original Message-----
From: "Richard Gaylord" <[email protected]>
Sent: 10/8/2013 4:56 PM
To: "[email protected]" <[email protected]>
Subject: [Jprogramming] pattern matching in J
*sorry it took me so long to return to the thread. i've been dealing with
health issues again (my advice is not to develop an autoimmune neurological
disease).*
*
*
*the responses to my question about pattern matching in J really do not
address the matter. the code given by roger*
*
*
*p,|."1 p
*
* /:~p,|."1 p *
*
*
*is very clever but it does not, as best i can tell, directly employ
pattern matching while my WL code does. *
*
*
*also, it does not start at the same point (use the same input) as the WL
code, nor does it end at the same point (with the same output) as te WL
code. *
*
*
*perhaps it is my fault becuase of the specific example i used but let me
restate it exactly as the corresponding code needs to appear and tn perhaps
the code to accomplish the same task in J can be posted *
*
*
*(but it must have the identical Input [1] and Output [2] as the WL code
(starting with p - which is not the same as society - is not acceptable
(although i can certainly convert society into p in WL). it must start
with society (or whatever you want to call it as long as it has the
identical structure)*
*
*
*Input[1]*
*
*
*society = {{1, {5, 7}}, {2, {}}, {3, {}}, {4, {1, 2}}, {5, {2, 7}}, {6,
{2}}, {7, {8}}, {8, {5}}};*
*
*
*note: becuase of my use of the semi-colon, Input [1] does not return any
Output*
*
*
*note: the 2nd element in each list in society is not necessarily limited
to an empty list and a 2-element list. it just happened to be that way.*
*
*
*Input [2]*
*
*
*symSoc = society /.*
* {a_Integer, b_} :> {a, Union[b, Cases[society, {x_, {___, a, ___}} :>
x]]}*
*
*
*Output [2]*
*
*
*{{1, {4, 5, 7}}, {2, {4, 5, 6}}, {3, {}}, {4, {1, 2}}, {5, {1, 2, 7,
8}}, {6, {2}}, {7, {1, 5, 8}}, {8, {5, 7}}}*
*
*
*as for roger's question as to what the WL code is doing:*
*
*
*we start in society with a list which contains a list *
*for each person in the system (so if there are 8 people in society, the
list consists of 8 elements, each a list.*
*
*
*e.g.. for person A we have **{A, {B,**R,E}} **the first element is the
person's name and the second element in each list is a list of the names of
the friends of A (let's say they are B, R, E).*
* *
*using symSoc, we look at the list of each and every person in the system.
specifically, we look at the 2nd element for each person to see if the name
A appears anywhere in another person's list of friends e.g. look at the
list of friends of person D, and if A is in the list, then the name D is
added to the list of friends of A. *
*
*
*since it may be that D is already listed as a friend of A, D would appear
twice in the list of of A's friends so we use the Union command to set rid
of the duplicates of names and sort them as well.*
*
*
*i hope this explanation is clear (i don't usually explain what what my
code is doing; some of the functions i like to create are nested anonymous
(or pure) functions that are virtually unreadable, even to me a day or so
after i've created them).*
*
*
*finally, let me note that anything that can be done in J can be done in WL
and in this case, roger took my example and without using what i call
pattern matching accomplished the same thing. but there are cases where
pattern matching is, if not the only way to go, it is much easier to write
the code for.*
*
*
*i hope this explains what's going on and what i' m asking about. if you go
to my note set (available as a pdf at
http://library.wolfram.com/infocenter/MathSource/5216/ you'll find a
tutorial on WL and the last section includes the code above and more code
(i especially like the SocNetFunction i created which is a nested anonymous
function*
*
*
*socNetFunction[rule_] := Function[y, Map[rule[Join[{#}, y[[#[[2]] ]] ] ]
&, y] ]*
*
*
*
*
* *
--
*"Those who would sacrifice freedom for security deserve neither." -
Benjamin Franklin*
*"I think that the very notion that equations are a good approach to
describing the natural world is a little bizarre."
- Stephen Wolfram*
*
*
nested
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm