So, I have an example of a list of items like this
(define A (list 'a 'c 'd 'e 'f 'e 'a))
Now I want to make a rating from this example
(define (scan lst) (foldl (lambda (element a-hash) (hash-update a-hash element add1 0)) (hash) lst))
The result should be like this:
>
Since the scan function will create a hash table containing unique keys and the number of repetitions of this key (if it catches an unindexed key, it will create a new location for this new key, starting from 0).
Then I would like to sort this hash table because it is unsorted:
(define (rank A) (define ranking (scan A)) (sort ranking >
Thus, the result will look like this:
# ((a. 2) ('e. 2) (' f. 1) ...)
Now I would like to truncate the hash table and throw the bottom to the threshold n = 1 (otherwise, we will select only elements with more than two repetitions).
(define (truncate lst n) (define l (length lst)) (define how-many-to-take (for/list ([il]
Thus, the result may look like this:
(('a. 2) (' e. 2))
However, on a large scale, this procedure is not very effective, it takes too much time. Will you offer better performance?
Thank you very much,
Part 2:
I have this data structure:
(automaton x (vector (state y (vector abc)) (state y (vector abc)) ...))
Then I randomly generate a population of 1000 of them. Then I browse and classify them using the above functions. If I scan them as is, it will take a long time. If I try to flatten them into a list like this
(list xyabcyab c...)
it will take even more time. Here is the smoothing function:
(define (flatten-au au) (match-define (automaton x states) au) (define l (vector-length states)) (define body (for/list ([i (in-range l)]) (match-define (state yz) (vector-ref states i)) (list y (vector->list z)))) (flatten (list x body)))
The scan function will look a little different:
(define (scan population) (foldl (lambda (auto a-hash) (hash-update a-hash (flatten-automaton auto) add1 0)) (hash) population))