简体   繁体   English

在球拍中,如何使用递归求和列表中的交替值

[英]In Racket how do i sum the alternating values in a list using recursion

I am writing a function using recursion that is supposed to add the alternating values in a list. 我正在使用递归编写一个函数,该函数应该在列表中添加交替值。 It's relatively simple to just write some code to add all the values in the list but I am struggling to only add the alternating values in the list. 仅编写一些代码以将所有值添加到列表中相对简单,但我正努力仅在列表中添加交替值。 If possible I would strongly prefer the code written using recursion over higher-order functions for this problem. 如果可能的话,相对于高阶函数,我强烈希望使用递归编写的代码来解决此问题。

My only guess for this problem is to manipulate the length of the list to only add the alternating values of the list or to possibly find a way to only add the odd elements in the list but I have no clue if this is even possible and if it was even possible i would have no clue where to even begin. 我对此问题的唯一猜测是操纵列表的长度以仅添加列表的交替值,或者可能找到一种仅在列表中添加奇数元素的方法,但是我不知道这是否可能,以及甚至有可能我不知道从哪里开始。

This is my code so far 到目前为止,这是我的代码

(define (skip-sum L)
  (cond
    [(empty? L) 0]
    [else (+ (first L) (skip-sum (rest L)))]))

This is what the results should look like. 这就是结果的样子。 As you can see only the odd elements in the lists were summed showing only alternating values in the list were added. 如您所见,仅对列表中的奇数元素求和,显示仅添加了列表中的交替值。

(check-expect (skip-sum (list 4 6 8)) 12)
(check-expect (skip-sum (list 1 3 5 7 9 11)) 15)
(check-expect (skip-sum (list 2 10 4 12 6 14 8 12 10))30)

For instance for the second example the alternating values which were added are 1+5+9=15. 例如,对于第二个示例,相加后的交替值为1 + 5 + 9 = 15。

Here is a possibile recursive definition: 这是一个可能的递归定义:

(define (skip-sum l)
  (cond ((empty? l) 0)
        ((empty? (cdr l)) (car l))
        (else (+ (car l) (skip-sum (cddr l))))))

Note that there are two cases to end the recursion: when the list is empty, or has only one element. 请注意,有两种情况可以结束递归:当列表为空或仅包含一个元素时。

In the recursive case we simply sum the first element of the list with the result of calling the function on the list starting from the third one (and so ignoring the second one). 在递归的情况下,我们简单地将列表的第一个元素与从第三个元素开始调用列表上的函数的结果相加(因此忽略第二个元素)。 In this way we skip every even element and obtain the correct sum. 这样,我们跳过每个偶数元素并获得正确的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用递归的球拍中,如果列表“ L”的总和为n但L中的值没有重复,我如何返回#true - In Racket using recursion how do I return #true if a list “L” sums to n but no values in L are duplicated 如何使用递归删除 Racket 中列表中的第一个和最后一个元素 - How do I remove the first and last element in a list in Racket using recursion 在Racket中,如何使用struct而不是仅使用高阶函数或递归来查找列表的长度 - In Racket how do i find the length of a list using struct instead of just using only higher-order functions or recursion 你如何将结构列表中的数字相加? (球拍) - How do you sum numbers in a list of structs? (Racket) 在球拍中,如何仅使用string-> list或list-> string函数替换字符串中的单词? - In racket how do I replace word in string using string->list or list->string function only? 如何将字符串列表转换为 Racket 中的列表列表 - How do i convert a list of strings into a list of lists in Racket 如何在球拍中将列表作为 arguments 的列表传递? - How do I pass a list as a list of arguments in racket? 如何使用递归构建嵌套链表? - How do I build a nested linked list using recursion? "如何在 Python 中使用递归来反转列表?" - How do I reverse a list using recursion in Python? 如何从关联列表中获取密钥? 方案/球拍 - How do I get the keys from an association list? scheme/ racket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM