在列表中找到元素的索引 [英] Find the index of element in list
本文介绍了在列表中找到元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取方案列表中元素的索引.例如:
I need to get index of element in list in scheme. For example:
(... 2'(2 3 4 5))
(... 2 '(2 3 4 5))
0
(... 4'(2 3 4 5))
(... 4 '(2 3 4 5))
2
有人可以帮忙吗?
推荐答案
我的最终解决方案:
(define index
(lambda (cislo l)
(if (equal? (car l) cislo) 0 (+ 1 (index cislo (cdr l))))))
(define map-index-pred
(lambda (pred? f l)
(foldr (lambda (x y)
(if (pred? (index x l))
(cons (f x) y) (cons x y))) '() l)))
这篇关于在列表中找到元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文