Write a Racket function
count-occurrences
that consumes two lists of symbols and produces a list of natural numbers measuring how many times items in the first list occur in the second list. For example:(count-occurrences (list 'a 'b 'a 'q) (list 'r 'a 'b 'e 'b 'g))
=> (list 1 2 1 0)
I've been struggling with this question - how do I use map
to do it, since for this question it's specified we can't use recursion.
My original idea was to do the following:
(define (count-occurrences los1 los2)
(map
(length (filter (lambda (x) (symbol=? x (first los1))) los2))
los1))
but using length
here can only get us the number 'a occurred, instead of going into recursion. and for abstract functions there can only be one argument for the inside function, so I'm totally lost.
map
's first argument should be a procedure. Sincelength
produces a numeric result,(map num lst)
will give you amap: contract violation
error. – assefamaru