Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

#### Understanding count and take from basics.lib

In basics.lib, we have the following definition of count:

```  count ((xs, xxs)) = 1 + count(xxs);
count (xx) = 1;
```
This definition uses pattern matching to count the number of block diagrams in parallel. For example count((6,5,4)) evaluates to 3. The first pattern recognizes a parallel arrangement of two block diagrams, while the second pattern will match any block diagram. In the multi-element case, the list is parsed as its first element in parallel with a block diagram consisting of all remaining elements (analogous to CAR and CDR in the Lisp programming language). Note that (a,b,c,d) matches (xs,xxs) as ((a),(b,c,d)).

Also in basics.lib, we have the following definition of take:

```  take (1, (xs, xxs)) = xs;
take (1, xs) = xs;
take (nn, (xs, xxs)) 	= take (nn-1, xxs);
```
This definition uses pattern matching to return the specified element. For example take(2,(6,5,4)) yields 5. The extra parentheses around (xs,xxs) avoid the structure of mere formal arguments separated by commas.

Note that take is 1-based while seq and par et al. are 0-based.

Next  |  Prev  |  Up  |  Top  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search