I have a function in Racket which takes a list, iterates through it, checks each item in the list against a condition, and is intended to output a list of the index of every item for which this condition is true. I cannot use indexes-of.
Currently, my setup is:
(define (function lst)
(define output empty)
(for ([i (length lst)])
(cond
[(CONDITION) (append output (list i))]
)
)
output
)
This outputs an empty list. I (think I) understand the reason why; append doesn't actually append to the variable output (the way one would expect in, say, Python), rather concatenates its parameters as a new list. I do not know how to fix this. Again, I'm trying to fetch indexes, so it's important those remain intact.
I have verified my condition functions properly; this does correctly identify each item, it's getting it into a returnable list that's the issue at this juncture.
Lists in Racket (Unlike regular Scheme) are immutable, and append
in both always returns a fresh list anyways. The plain for
comprehension you're using doesn't return any value, either.
There's quite a few ways to do what you want without indexes-of
(Though that's the easy way). For example, using for/list
with a #:when
clause to select just the elements that CONDITION
returns true for:
(define (function lst)
(for/list ([elem (in-list lst)]
[i (in-naturals)]
#:when (CONDITION elem))
i))