scheme - Returning number of times atoms in one list appear in another list -


i working through introduction scheme course , have been stuck on particular problem number of hours. requirement write function takes 2 lists of atoms, lat1 , lat2, , returns total number of times atoms in lat1 occur in lat2.

for example (occurn '(d e f) '(a x b e d g h f e)) return 4

so far code have is

         (define occurn           (lambda (lat1 lat2)            (cond             ((null? lat1) 0)            ((null? lat2) 0)            (else               (cond               ((eq? (car lat1) (car lat2)) (add1 (occurn lat1 (cdr lat2))))                 (else (occurn lat1 (cdr lat2)))))))) 

this works going through first element of lat1, checking see if every element of lat2 equal , adding 1 count if are, otherwise moving on next element. problem i'm having getting code start on next value of lat1 , following values of lat1 while retaining count , going through list of lat2 , checking equivalence other values of lat1 other first, further adding count if . if has hints how can proceed problem it'd appreciated.

assuming have count returns number of times element occurs in list:

(define (occurn lat1 lat2)   (apply + (map (lambda (elt1)                    (count elt1 lat2))                  lat1))) 

recognizing need count, 1 step in producing occurn, important step in decomposing problem.

here tail-recursive count:

(define (count elt lst) ; predicate eq?   (let counting ((lst lst) (n 0))     (if (null? lst)         n         (counting (cdr lst)                   (if (eq? elt (car lst))                       (+ n 1)                       n))))) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -