1 | count = (p) ->
|
2 | if (iscons(p))
|
3 | n = 0
|
4 | while (iscons(p))
|
5 | n += count(car(p)) + 1
|
6 | p = cdr(p)
|
7 | else
|
8 | n = 1
|
9 | return n
|
10 |
|
11 | # this probably works out to be
|
12 | # more general than just counting symbols, it can
|
13 | # probably count instances of anything you pass as
|
14 | # first argument but didn't try it.
|
15 | countOccurrencesOfSymbol = (needle,p) ->
|
16 | n = 0
|
17 | if (iscons(p))
|
18 | while (iscons(p))
|
19 | n += countOccurrencesOfSymbol(needle,car(p))
|
20 | p = cdr(p)
|
21 | else
|
22 | if equal(needle,p)
|
23 | n = 1
|
24 | return n
|
25 |
|
26 |
|
27 | # returns the total number of elements
|
28 | # in an expression
|
29 | countsize = (p) ->
|
30 | n = 0
|
31 |
|
32 | if (istensor(p))
|
33 | for i in [0...p.tensor.nelem]
|
34 | n += p.tensor.elem[i]
|
35 | else if (iscons(p))
|
36 | while (iscons(p))
|
37 | n += count(car(p)) + 1
|
38 | p = cdr(p)
|
39 | else
|
40 | n = 1
|
41 |
|
42 | return n
|
43 |
|