|
Chapter 18: Sets and ClassesIn this chapter we look at more of the built-in functions of J. The connecting theme is, somewhat loosely, working with sets and classes. Suppose that, for some list, for the purpose at hand, the order of the items is irrelevant and the presence of duplicate items is irrelevant. Then we can regard the list as (representing) a finite set. In the abstract, the set 3 1 2 1 is considered to be the same set as 1 2 3. The word "class" we will use in the sense in which, for example, each integer in a list belongs either to the odd class or to the even class. 18.1 Sets18.1.1 MembershipThere is a built-in verb e. (lowercase e dot, called "Member"). The expresssion x e. y tests whether x matches any item of y, that is, whether x is a member of the list y. For example:
Evidently the order of items in y is irrelevant and so is the presence of duplicates in y.
We can test whether a table contains a particular row:
18.1.2 LessThere is a built-in verb -. (minus dot, called "Less"). The expression x -. y produces a list of the items of x except those which are members of y.
Evidently the order of items in y is irrelevant and so is the presence of duplicates in y. 18.1.3 NubThere is a built-in verb ~. (tilde dot, called "Nub"). The expression ~. y produces a list of the items of y without duplicates.
We can apply nub to the rows of a table:
18.1.4 Nub SieveThe verb "nub sieve" (~:) gives a boolean vector which is true only at the nub.
18.1.5 Functions for SetsThe customary functions on sets, such as set-union, set-intersection or set-equality, are easily defined using the built-in functions available. For example two sets are equal if all members of one are members of the other, and vice versa. seteq =: *./ @: (e. , e.~)
18.2 The Table AdverbRecall that the adverb / generates a verb; for example +/ is a verb which sums lists. More precisely, it is the monadic case of +/ which sums lists. The dyadic case of +/ generates a table:
The general scheme is that if we have z =: x f/ y then z is a table such that the value at row i column j is given by applying f dyadically to the pair of arguments i{x and j{y. That is, z contains all possible pairings of an item of x with an item of y. Here is another example:
The result shows, in the first row, the value of 'a' = 'face', in the second row the value of 'b' ='face' and so on. 18.3 Classes18.3.1 Self-ClassifyConsider the problem of finding the counts of letters occurring in a string (the frequency-distribution of letters). Here is one approach. We form a table testing each letter for equality with the nub.
The expression ((nub y) = / y) can be abbreviated as (= y). The monadic case of the built-in verb = is called "Self-classify").
If we sum each row of = y we obtain the counts, in the order of the letters in the nub.
The counts can be paired with the letters of the nub:
18.3.2 Classification SchemesGardeners classify soil-types as acid, neutral or alkaline, depending on the pH value. Suppose that a pH less than 6 is classed as acid, 6 to 7 is neutral, and more than 7 as alkaline. Here now is a verb to classify a pH value, returning A for acid, N for neutral and L for alkaline (or limy). classify =: ({ & 'ANL') @: ((>: & 6) + (> & 7))
The classify function we can regard as defining a classification scheme. The letters ANL, which are in effect names of classes, are called the keys of the scheme. 18.3.3 The Key AdverbGiven some data (a list, say), we can classify each item to produce a list of corresponding keys.
We can select and group together all the data in, say, class A (all the data with key A):
Now suppose we wish to count the items in each class. That is, we aim to apply the monadic verb # separately to each group of items all of the same key. To do this we can use the built-in adverb /. (slash dot, called "Key").
For another example, instead of counting the members we could exhibit the members, by applying the box verb <.
The verb we apply can discover for itself the class of each separate argument, by classifying the first member: Here the verb u produces a boxed list: the key and count: u =: (classify @: {.) ; #
The general scheme for the "Key" adverb is as follows. In the expression x u /. y, we take y to be a list, and x is a list of keys of corresponding items of y according to some classification scheme, and u is the verb to be applied separately to each class. The scheme is: x u /. y means (= x) (u @ #) y To illustrate: y =: 4 5 6 7 8 x =: classify y u =: <
We see that each row of =x selects items from y, and u is applied to this selection. 18.3.4 Letter-Counts RevisitedRecall the example of finding the counts of letters in a string.
Here is a variation. We note that we have in effect a classification scheme where we have as many different classes as different letters: each letter is (the key of) its own class. Thus we can write an expression of the form y u /. y. The applied verb u will see, each time, a list of letters, all the same. It counts them, with #, and takes the first, with {., to be a label for the class. u =: {. ; #
This is the end of Chapter 18 |
Copyright © Roger Stokes 2000. This material may be freely reproduced, provided that this copyright notice and provision is also reproduced.
last updated 17 Mar 00