尝试从方案中的列表中获取元素的索引

发布于 2024-10-25 03:33:58 字数 262 浏览 1 评论 0原文

所以我试图从列表中获取索引 ex:

(get-indices 'G (list 'A 'G 'T 'X 'I 'T 'G))

(2 7)

其中索引从 1 开始,所以 ' A 是索引一

我正在考虑使用辅助函数,它需要 elt lst 和索引 ex: (get-indices-helper el lst index)

我也在考虑可能使用 list-ref 并喜欢切换它以使其以获取索引的方式工作,但是我找不到它的实际方案定义。

So i'm trying to get the indices from a list ex:

(get-indices 'G (list 'A 'G 'T 'X 'I 'T 'G))

(2 7)

where the index starts at 1 so 'A is index one

I was thinking on using a helper function where it takes an elt lst and index
ex: (get-indices-helper el lst index)

I was also thinking about possibly using list-ref and like switching it to make it work in the get indices way however i could not find the actual scheme definition for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-11-01 03:33:58

编写一个函数,递归输入列表,跟踪它正在查看的元素的位置,并使用 cons 发出匹配索引。这实在是微不足道的事;我想这是一个你被设置为家庭作业的问题?

; Walk down the list given in haystack, returning a list of indices at which
; values equal? to needle appear.
(define (get-indices needle haystack)
  ; Loop along the haystack.
  (define (loop rest-of-haystack index)
    ; If the haystack is empty, return the empty list.
    (if (null? rest-of-haystack) '()
      ; Recurse to the next position in the list.
      (let ((rest-of-indices (loop (cdr rest-of-haystack) (+ index 1))))
        (if (equal? (car rest-of-haystack) needle)
          ; If haystack is here, emit the current index.
          (cons index rest-of-indices)
          ; Otherwise, return rest-of-indices.
          rest-of-indices))))
  ; Run the loop defined above, from the beginning of haystack, with
  ; the first element being assigned an index of 1.
  (loop haystack 1))

使用 GNU Guile 或 MzScheme 或其他东西对此进行测试:

(display (get-indices 'G (list 'A 'G 'T 'X 'I 'T 'G))) (newline)
(display (get-indices 1 (list 1 1 1 2 1 3))) (newline)

打印:

(2 7)
(1 2 3 5)

耶!

Write a function that recurses down the input list, keeping track of the position of the element that it's looking at, and emitting the matching indexes with cons. This is really trivial; I assume that it is a question that you have been set as homework?

; Walk down the list given in haystack, returning a list of indices at which
; values equal? to needle appear.
(define (get-indices needle haystack)
  ; Loop along the haystack.
  (define (loop rest-of-haystack index)
    ; If the haystack is empty, return the empty list.
    (if (null? rest-of-haystack) '()
      ; Recurse to the next position in the list.
      (let ((rest-of-indices (loop (cdr rest-of-haystack) (+ index 1))))
        (if (equal? (car rest-of-haystack) needle)
          ; If haystack is here, emit the current index.
          (cons index rest-of-indices)
          ; Otherwise, return rest-of-indices.
          rest-of-indices))))
  ; Run the loop defined above, from the beginning of haystack, with
  ; the first element being assigned an index of 1.
  (loop haystack 1))

Testing this with GNU Guile or MzScheme or something:

(display (get-indices 'G (list 'A 'G 'T 'X 'I 'T 'G))) (newline)
(display (get-indices 1 (list 1 1 1 2 1 3))) (newline)

Prints:

(2 7)
(1 2 3 5)

Yay!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文