scheme - DrRacket : How to get the position of a value in a list -
i trying list of positions of value in list in intermediate student language.
for instance wish list of positions value "a" in following list
(list false false false false false false false false )
the output must
(list 1 6)
i'll give hints solve problem, it's better if reach solution own means. fill-in blanks:
; position procedure ; lst: input list ; ele: searched element ; idx: initial index, starts in 0 (define (position lst ele idx) (cond (<???> ; if input list empty <???>) ; we're done, return empty list (<???> ; if current element equals 1 we're looking (cons <???> ; build output list, cons index found (position <???> ele <???>))) ; , advance recursion (else ; otherwise (position <???> ele <???>)))) ; advance recursion
notice idx
parameter necessary keep track of index we're over, starting @ zero. when recursion advances, must advance both input list , index. don't forget test procedure:
(position '(false false false false false false false false) 'a 0) => '(1 6)
Comments
Post a Comment