Well there are a few things we can quickly understand, the first is that you're going to need to recurse through your initial list, and the second is that you're going to need to keep an accumulator list, and somehow have a notion of what element of the first list you're looking at, so we can also add a counter.
So,
; listsearch : (listof Any) Any -> (listof Int)
; Searches a list for val and returns a list of indexes for occurrences of val in lst
; (listsearch '(1 2 1 3 4 1) 1) => '(0 2 5)
(define (listsearch lst val)
(local [(define (helper lst acc counter)
(cond [(empty? lst) acc]
[(equal? val (first lst)) (helper (rest lst)
(cons counter acc)
(add1 counter))]
[else (helper (rest lst) acc (add1 counter))]))]
(reverse (helper lst empty 0))))
I've added a local because the counter should be present, but we want the actual function to be tidy, so the call is simply requiring a list and a value.
This simply goes through the list one by one, and makes three checks
- Is the list empty? Return my accumulated list (base is empty)
- Is the first item in the list my value? Start again but add that value to my accumulator and add one to my counter
- Is the first item in the list something else? Start again but add one to my counter
This results in a backwards list, so we reverse it at the end.
That's it! :)