计划如何创建列表

发布于 2024-08-26 21:06:08 字数 39 浏览 5 评论 0原文

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

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

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

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

发布评论

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

评论(3

北方的韩爷 2024-09-02 21:06:08

根据看到您的其他一些问题,我认为您可能很难理解像Scheme 这样的函数式语言的核心概念。

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

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

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

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

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

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

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

这是一个使用数字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)

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

这是另一个例子。这个例子使用我们的 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

同样,基本情况和递归情况都通过计算生成列表的表达式来返回列表。

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

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.

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.

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:

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:

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

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

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

Here's a function that consumes a number n and builds the list (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 add5 function to add 5 to each element of a list of numbers (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.

千柳 2024-09-02 21:06:08

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

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

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

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

(returnlist 2 3 4)

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

You probably want simply: '(2 3 5 7 11) or (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 '()))

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)

一个人的旅程 2024-09-02 21:06:08

另一种不太为人所知的方法是:

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

即“list”函数本身可以定义为:

> (define list (lambda x x))

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