查找列表中元素的索引

时间:2016-08-20 18:37:57

标签: scheme racket

我可以使用findf根据某个过程查找列表中的元素:

(define sl '("tester" "testing"  "other words" "for test of" ))
(findf (lambda (x) (string-contains? x "test")) sl) 

输出:

'("tester" "testing" "for test of")

如何获得满足某些返回true和false的过程的元素的索引,例如" string-contains?"以上 ?我希望在上面的例子中有以下输出:

'(0 1 3)

1 个答案:

答案 0 :(得分:2)

#lang racket

(define (find strings needle)
  (for/list ([s strings]
             [i (in-naturals)]
             #:when (string-contains? s needle))
    i))

(find '("tester" "testing"  "other words" "for test of")
      "test")