list Scheme如何创建一个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2542213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:28:37  来源:igfitidea点击:

Scheme how to create a list

listscheme

提问by poorStudent

Okay this may sound like a ridiculous question, but how do you return a list in scheme.?

好吧,这听起来可能是一个荒谬的问题,但是您如何返回方案中的列表。?

回答by Barry Brown

Based on seeing some of your other questions, I think you may be having trouble getting your head wrapped around the concepts central to a functional language such as Scheme.

基于看到您的其他一些问题,我认为您可能无法理解功能语言(如 Scheme)的核心概念。

At the level you're learning Scheme (novice), every function you write has an input and an output, and the body of every function is a single expression. Whatever value that expression evaluates to is returned by the function. There is no need to explicitly "return" anything as you would in an imperative language like Java or C; it just happens as a direct consequence of evaluating the expression.

在您学习 Scheme(新手)的级别,您编写的每个函数都有一个输入和一个输出,并且每个函数的主体都是一个单独的表达式。表达式计算的任何值都由函数返回。不需要像 Java 或 C 这样的命令式语言那样显式地“返回”任何东西;它只是作为评估表达式的直接结果而发生的。

The body of a function is a single expression. It's not like Java where the body of a method consists of a series of instructions:

函数体是单个表达式。它不像 Java 那样方法体由一系列指令组成:

do this
then do that
then do something else
then return something (maybe)

Scheme functions evaluate a single expression; nothing more. Here's a simple function that adds 5 to whatever number is passed as an argument:

Scheme 函数计算单个表达式;而已。这是一个简单的函数,它将作为参数传递的任何数字加 5:

(define (add5 x)
  (+ x 5))

The body of the function is (+ x 5), which is just an expression to be evaluated. The value of xis plugged in, the +(addition) function is applied to xand 5, and the result is returned.

函数的主体是(+ x 5),它只是一个要计算的表达式。x插入的值,+x和5应用(加法)函数,返回结果。

Lists aren't much different. All you need is an expression that will construct a list. Two have already been mentioned: listis used to build a list from scratch if you already have all the elements; consis used to add a single element to an existing list and is often used recursively.

列表没有太大的不同。您所需要的只是一个构建列表的表达式。已经提到了两个:如果您已经拥有所有元素,则列表用于从头开始构建列表;cons用于将单个元素添加到现有列表中,并且经常递归使用。

Here's a function that consumes a number nand builds the list (n n-1 n-2 ... 0)

这是一个使用数字n并构建列表的函数(n n-1 n-2 ... 0)

(define (makelist n)
  (if (= n 0)
     (list 0)                       ; base case. Just return (0)
     (cons n (makelist (- n 1)))))  ; recursive case. Add n to the head of (n-1 n-2 ... 0)

In both the base and recursive cases, a list is returned by simply evaluating an expression that uses one of the list-building functions.

在基本和递归情况下,通过简单地计算使用列表构建函数之一的表达式来返回列表。

Here's another example. This one uses our add5function to add 5 to each element of a list of numbers (lon):

这是另一个例子。这个使用我们的add5函数将 5 添加到数字列表 (lon) 的每个元素:

(define (add5list lon)
  (if (null? lon)
    `()                 ; base case: lon is empty. Return an empty list.
    (cons (add5 (car lon)) (add5list (cdr lon)))))  ; recursive case.
                                                    ; Add 5 to the head of lon and prepend it to the tail of lon

Again, both the base and recursive cases are returning lists by evaluating expressions that result in lists.

同样,基本情况和递归情况都通过评估导致列表的表达式来返回列表。

The key thing to remember about Scheme is all functions return something, and that something is simply the result of evaluating an expression. The body of a Scheme function is a single expression.

关于Scheme,要记住的关键是所有函数都返回一些东西,而这些东西只是对表达式求值的结果。Scheme 函数的主体是单个表达式。

回答by Brian R. Bondy

You probably want simply: '(2 3 5 7 11)or (list 2 3 5 7 11)?

您可能只想要:'(2 3 5 7 11)(list 2 3 5 7 11)?

You can also construct lists by specifying an element and a list to add it to: (cons 2 (cons 3 '()))

您还可以通过指定一个元素和一个列表来构造列表以将其添加到: (cons 2 (cons 3 '()))

Here's an example of returning a list from a function:

这是从函数返回列表的示例:

(define returnlist 
  (lambda(a b c) 
    (cons a (cons b (cons c '())))
))

(returnlist 2 3 4)

Return value will be the list: (list 2 3 4)

返回值将是列表: (list 2 3 4)

回答by Paul Hollingsworth

Another not-so-well known way to do this:

另一种不太知名的方法来做到这一点:

> ((lambda x x) 2 3 5 7 11)
(2 3 5 7 11)

that is, the "list" function itself can be defined as:

也就是说,“列表”函数本身可以定义为:

> (define list (lambda x x))