帮助创建Scheme图-多种功能

发布于 2024-11-08 06:29:43 字数 485 浏览 0 评论 0原文

我应该定义一个函数,该函数将函数列表和另一个列表作为参数,并返回通过按顺序将所有函数应用于列表元素而获得的值列表。

我想出了以下内容,但收到错误:

+: expects type <number> as 1st argument, given: (1 2 3); other arguments were: 1

当我尝试将函数与示例输入一起使用时, (map-many (list (lambda (x) (+ x 1)) (lambda (x) ( * xx))) '(1 2 3))。任何建议将不胜感激。

(define (map-many fun-list lst)
    (if (null? lst) lst
        (map ((car fun-list) lst)
             (map-many (cdr fun-list) lst))))

I am supposed to define a function that takes as arguments a list of functions and another list and returns a list of values obtained by applying all the functions, in sequence, over the elements of the list.

I came up with the following but I receive the error:

+: expects type <number> as 1st argument, given: (1 2 3); other arguments were: 1

when I try to use the function with the sample input, (map-many (list (lambda (x) (+ x 1)) (lambda (x) (* x x))) '(1 2 3)). Any suggestions would be appreciated.

(define (map-many fun-list lst)
    (if (null? lst) lst
        (map ((car fun-list) lst)
             (map-many (cdr fun-list) lst))))

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

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

发布评论

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

评论(3

匿名的好友 2024-11-15 06:29:43
(define (map-many fun-list lst)
  (if (null? fun-list) lst
      (map (car fun-list)
           (map-many (cdr fun-list) lst))))

您的错误是:

  1. 您在 (null? lst) 而不是 (null? fun-list) 上完成了递归。
  2. 该函数的其余部分不符合逻辑。
(define (map-many fun-list lst)
  (if (null? fun-list) lst
      (map (car fun-list)
           (map-many (cdr fun-list) lst))))

Your errors were:

  1. You finish your recursion on (null? lst) instead of (null? fun-list).
  2. The rest of the function was not logical.
缱倦旧时光 2024-11-15 06:29:43

您向 map 传递了错误的函数。不要传递 ((car fun-list) lst),而是尝试仅传递 (car fun-list)

You're passing the wrong function to map. Instead of passing ((car fun-list) lst), try passing just (car fun-list).

坚持沉默 2024-11-15 06:29:43

你的措辞对我来说有点不清楚。您应该执行以下操作吗?有两个列表,一个是过程(我们称之为列表 P),另一个是值(我们称之为列表 V)。所以你要找到一个列表,其中:

  • list-ref 0 = (P0 V0)
  • list-ref 1 = (P1 (P0 V1))
  • list-ref 2 = (P2 (P1 (P0 V2))

等等?

(define (map-many procs vals)
   (let ((applied (map (car procs) vals))
     (if (null? vals)
         vals
         (cons (car applied) (map-many (cdr procs) (cdr applied)))))))

Your wording is a bit unclear to me. Are you supposed to do the following? Have two lists, one of procedures (let's call this list P), another with values (let's call this list V). So you're looking to then find a list where:

  • list-ref 0 = (P0 V0)
  • list-ref 1 = (P1 (P0 V1))
  • list-ref 2 = (P2 (P1 (P0 V2))

and so on?

(define (map-many procs vals)
   (let ((applied (map (car procs) vals))
     (if (null? vals)
         vals
         (cons (car applied) (map-many (cdr procs) (cdr applied)))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文