绝对第一次使用方案,简单列表操作

Rya*_*man 2 scheme list addition

我对计划一无所知,我觉得一旦我回答这个问题,其余的功课应该顺利进行.

我正在定义一个以列表作为唯一参数的函数,然后返回相同的列表,其中第一个元素添加到其余元素中.例如:

(addFirst ‘(4 3 2 1))   =>   (8 7 6 5)
Run Code Online (Sandbox Code Playgroud)

我觉得我应该在这里使用地图和汽车功能......但我似乎无法完全正确.我当前版本的代码如下所示:

(define (addlist x) ;adds the first element of a list to all other elements
  (define a (car x)) ;a is definitely the first part of the list
  (map (+ a) x)
)
Run Code Online (Sandbox Code Playgroud)

如何使添加功能以这种方式工作?显然我不能提供列表作为参数,但我应该再次使用汽车还是递归?

好的,对于后代,这是完整的,正确的,格式化的代码:

(define (addlist x) ;adds the first element of a list to all other elements
  (define a (car x)) ;a is definitely the first part of the list
  (map (lambda (y) (+ a y)) x)
)
Run Code Online (Sandbox Code Playgroud)

Ros*_*son 8

Map需要一个函数和n个列表.因此,您需要将(+ a)转换为带有一个参数的函数(因为您希望映射到一个列表).

(map (lambda (item) ... do something to add a to item ...) x)
Run Code Online (Sandbox Code Playgroud)