I cant figure out this scheme code please help. Compute-frequencies takes in two separate lists looking-for-list and pool-list. It is supposed to return a list that shows how many times everything in looking-for-list is in pool-list.
I know I am close it is just some little error most likely having to do with the recursive call after making it through pool-list.
(define (compute-frequencies looking-for-list pool-list)
(define (helper looking-for-list pool-list current-frequency frequency-list) ; keeps track of finished list and iterates through both lists
(if (null? looking-for-list) (reverse frequency-list) ; finding the number of times data in looking-for-list are in pool-list
(if (null? pool-list)
(helper (cdr looking-for-list) pool-list 0 (cons current-frequency frequency-list))
(if (equal? (car looking-for-list) (car pool-list))
(helper looking-for-list (cdr pool-list) (+ 1 current-frequency) frequency-list)
(helper looking-for-list (cdr pool-list) current-frequency frequency-list)))))
(helper looking-for-list pool-list 0 '() ))
Alright reading your code it seems like your algorithm itself is perfectly fine. The problem is much simpler.
Look at your code, once you’ve checked all the elements of
pool-listfor the first element in yourlooking-for-listyou want to restorepool-listto its original state. To do this, you callhelperwithpool-listaspool-list. You almost certainly mean the one defined in thecompute-frequencyarguments but Scheme takes the one inhelper‘s arguments since it shadow’s the one incompute-frequency. This means that after the first iteration,pool-listis just'()so the frequency of everything else is 0.To solve this, do some renaming. In the future, try to remember that Scheme variables can and will shadow ones in a larger scope. And also, try using
condinstead of nested if statements. Much more readable in Scheme (it’s how I found this problem)