递归函数接受方案中的列表 [英] recursive function accepts list in scheme

查看:37
本文介绍了递归函数接受方案中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scheme 的新手,这是我的第一个函数式语言.递归地实现几乎所有东西对我来说似乎很尴尬.尽管如此,还是能够实现具有单个整数输入的阶乘和斐波那契问题的函数.

I'm new to Scheme and this is my very first Functional language. Implementing almost everything recursively seems to be awkward for me. Nevertheless, was able to implement functions of Factorial and Fibonacci problems having a single integer input.

然而,当你的函数有一个列表输入时呢?假设这个练习:

However, what about when your function has an input of a list? Suppose this exercise:

功能:ret10 - 提取所有大于 10 的数字并将其作为列表返回在给定列表中找到的,guile> (ret10 ‘(x e (h n) 1 23 12 o))输出:(23 12)

FUNCTION: ret10 - extracts and returns as a list all the numbers greater than 10 that are found in a given list, guile> (ret10 ‘(x e (h n) 1 23 12 o)) OUTPUT: (23 12)

我应该将 (define c(list)) 作为我函数的参数吗?或者还有其他方法吗?

Should I have (define c(list)) as the argument of my function in this? or is there any other way?

请帮忙.谢谢!

这是我根据 Óscar López 爵士在下面的回答得出的解决方案.希望这对其他人有所帮助:

Here's my derived solution based on sir Óscar López's answer below.. hope this helps others:

(define (ret10 lst)
    (cond
        ((null? lst) '())

        ((and (number? (car lst)) (> (car lst) 10))
            (cons (car lst)
            (ret10 (cdr lst))))

        (else (ret10 (cdr lst)))
    )
)

推荐答案

这种接收列表作为输入并返回另一个列表作为输出的问题具有一个众所周知的解决方案模板.我首先建议你看看 The Little Schemer如何设计程序,这两本书都会教你开始思考解决方案的正确方法.

This kind of problem where you receive a list as input and return another list as output has a well-known template for a solution. I'd start by recommending you take a look at The Little Schemer or How to Design Programs, either book will teach you the correct way to start thinking about the solution.

首先,我将向您展示如何解决一个类似的问题:复制一个列表,完全按照它来.这将展示解决方案的一般结构:

First, I'll show you how to solve a similar problem: copying a list, exactly as it comes. That'll demonstrate the general structure of the solution:

(define (copy lst)
  (cond ((null? lst)                ; if the input list is empty
         '())                       ; then return the empty list
        (else                       ; otherwise create a new list
         (cons (car lst)            ; `cons` the first element
               (copy (cdr lst)))))) ; and advance recursion over rest of list

现在让我们看看上述内容与您的问题有何关系.显然,递归的基本情况将是相同的.不同的是,我们cons 列表的其余元素如果它是一个数字(提示:使用数字? 过程)它大于10.如果条件不成立,我们只是推进递归,不考虑任何东西.这是总体思路,填空:

Now let's see how the above relates to your problem. Clearly, the base case for the recursion will be the same. What's different is that we cons the first element with the rest of the list only if it's a number (hint: use the number? procedure) and it's greater than 10. If the condition doesn't hold, we just advance the recursion, without consing anything. Here's the general idea, fill-in the blanks:

(define (ret10 lst)
  (cond (<???> <???>)          ; base case: empty list
        (<???>                 ; if the condition holds
         (cons <???>           ; `cons` first element
               (ret10 <???>))) ; and advance recursion
        (else                  ; otherwise
         (ret10 <???>))))      ; simply advance recursion

不要忘记测试它:

(ret10 '(x e (h n) 1 23 12 o))
=> '(23 12)

最后一点:通常您会使用 filter 过程来解决这个问题 - 该过程将一个列表作为输入并返回另一个列表作为输出,其中只有满足给定谓词的元素.在您学习并了解如何手动"编写解决方案后,请查看 filter 并使用它编写解决方案,只是为了比较不同的方法.

As a final note: normally you'd solve this problem using the filter procedure - which takes as input a list and returns as output another list with only the elements that satisfy a given predicate. After you learn and understand how to write a solution "by hand", take a look at filter and write the solution using it, just to compare different approaches.

这篇关于递归函数接受方案中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆