从列表中添加数字(例如 asdf125dkf 将返回 8)

发布于 2024-10-01 05:35:45 字数 417 浏览 2 评论 0原文

我需要一个函数,它将接受字符和数字列表,然后返回相加的数字(忽略字符)。这就是我到目前为止所得到的:

(define (adder lst)
   (cond
     ((null? lst)
       0)
     ((number? (car lst))
      (+(adder (car lst)) (adder (cdr lst))))
     ((char? (car lst))
      ((adder(cdr lst))))
     ))

(display (adder '(asd12sdf)))

在 codepad.org 上运行它只会显示 void。我知道代码是错误的,因为它看起来是错误的,但我不知道如何修复它...我如何让该函数跟踪它找到的第一个数字并将其添加到它找到的下一个数字,同时跳过所有数字人物?

I need a function that will take in a list of characters and numbers, and then return the numbers added up (ignoring the characters). This is what I have so far:

(define (adder lst)
   (cond
     ((null? lst)
       0)
     ((number? (car lst))
      (+(adder (car lst)) (adder (cdr lst))))
     ((char? (car lst))
      ((adder(cdr lst))))
     ))

(display (adder '(asd12sdf)))

Running it on codepad.org just displays void. I know the code is wrong because it looks wrong, but I have no idea how to fix it... How do I have the function keep track of the first number it finds and add it to the next one it finds, while skipping all characters?

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

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

发布评论

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

评论(3

我乃一代侩神 2024-10-08 05:35:45

在第二种情况下,没有理由在 (car lst) 上运行 adder 。只需将 (car list) 本身添加到递归步骤中就可以了。

对于最后一行,不要测试 (char? (car lst))。只需将最后一行设为 else 子句,这意味着除数字之外的任何内容都将转到 else 行。

您得到 void 的原因是因为您的输入不满足任何 cond 条件,并且您没有 else,因此答案是什么(即(void))。

最后一个错误在于您提供的输入中。 '(asd12sdf) 实际上是一个包含一个名为“asd12sdf”符号的列表。我想你想给它 '(asd 1 2 sdf) (6 个符号和 2 个数字的列表),结果应该是 3。请注意,符号 之间有一个非常重要的区别'a 和字符 #\a

看起来你已经掌握了逻辑,所以你的问题似乎不是函数式语言,而是Scheme的语法。

编辑:,在最后一行,你有 ((adder(cdr lst))) ,它周围有太多的括号。这将导致Scheme尝试将加法器的结果(这是一个数字)作为过程进行评估(错误!)。

In your second cond case, there's no reason to run adder on (car lst). Just adding (car list) itself to the recursive step should work.

For the last line, don't test (char? (car lst)). Just make the last line the else clause, meaning that anything BUT a number will go to the else line.

The reason you're getting void is because your input doesn't satisfy any of the cond conditions, and you have no else, so the answer is nothing (i.e. (void)).

The last mistake is in the input you're giving it. '(asd12sdf) is literally a list with one symbol named "asd12sdf". I think you want to give it '(a s d 1 2 s d f) (a list of 6 symbols and 2 numbers) which should result in 3. Notice that there's a very important difference between the symbol 'a and the character #\a.

It looks like you have the logic down, so your problem doesn't seem to be functional languages, just Scheme's syntax.

Edit: and in the last line, you have ((adder(cdr lst))) which has one too many parens wrapped around it. That will cause Scheme to attempt to evaluate the result of adder (which is a number) as a procedure (error!).

素衣风尘叹 2024-10-08 05:35:45

您应该观察到这个函数或多或少是 sum ,可以简单地使用 fold 来定义。

(define (adder lst)
   (fold + 0 lst))

折叠有什么作用?基本上,它的定义如下:(

(define (fold f initial lst)
  (if (null? lst)
     initial
     (fold f (f (car lst) initial) (cdr lst))))

换句话说,它在 lst 的每个元素上调用 f,一个有 2 个参数的函数,使用 lst 的汽车作为第一个参数,并将累积结果作为 f 的第二个参数。 )

这里您需要解决的问题是 + 不知道如何对非数字值进行操作。没问题,你已经处理过了。如果它是一个角色,会发生什么?好吧,您没有在总值中添加任何内容,因此将其替换为 0。因此,您的解决方案非常简单:

(define (adder lst)
   (fold your-new-protected-+ 0 lst))

You should observe that this function is more or less sum which can be defined simply by using fold.

(define (adder lst)
   (fold + 0 lst))

What does fold do? Basically, it's defined like so:

(define (fold f initial lst)
  (if (null? lst)
     initial
     (fold f (f (car lst) initial) (cdr lst))))

(In other words, it calls f, a function of 2 arguments, on each element of lst, using the car of the lst as the first argument, and the accumulated result as the second argument to f.)

The issue here which you need to address is that + doesn't know how to operate on non-numeric values. No problem, you've already dealt with that. What happens if it's a character instead? Well, you're not adding anything to the total value, so replace it with a 0. Therefore, your solution is as simple as:

(define (adder lst)
   (fold your-new-protected-+ 0 lst))
瑕疵 2024-10-08 05:35:45

在 Common Lisp 中:

(reduce #'+ '(1 #\a #\b 2 1 2 #\c #\d 4)
        :key (lambda (item) (if (numberp item) item 0)))

(loop for item in '(1 #\a #\b 2 1 2 #\c #\d 4)
      when (numberp item) sum item)

In Common Lisp:

(reduce #'+ '(1 #\a #\b 2 1 2 #\c #\d 4)
        :key (lambda (item) (if (numberp item) item 0)))

or

(loop for item in '(1 #\a #\b 2 1 2 #\c #\d 4)
      when (numberp item) sum item)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文