Reputation: 21
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 '() ))
Upvotes: 1
Views: 232
Reputation: 53871
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-list
for the first element in your looking-for-list
you want to restore pool-list
to its original state. To do this, you call helper
with pool-list
as pool-list
. You almost certainly mean the one defined in the compute-frequency
arguments but Scheme takes the one in helper
's arguments since it shadow's the one in compute-frequency
. This means that after the first iteration, pool-list
is 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 cond
instead of nested if statements. Much more readable in Scheme (it's how I found this problem)
Upvotes: 2