Make Scheme function for 3 parameters return function for the 3d

Make Scheme function for 3 parameters return function for the 3d

我很难将这个相当简单的 Scheme 函数转换为 returns 另一个函数接收列表并将前一个函数应用于该列表中的所有元素的函数。

这个函数

(define (operList op i lis) 

    (if (= 0 (length lis)) '() 

    (cons (op i (car lis)) (operList op i (cdr lis))))

)

可以这样调用

(operList + 2 '(1 1 1))

和returns'(3 3 3)

但是,我该如何编辑这个函数,以便我可以按以下方式调用它

((operList + 2) '(1 1 1)) 

结果相同

您必须 return 一个接收列表的新函数。我冒昧地修正了缩进和基本情况(这不是你应该如何询问列表是否为空!);特别注意现在调用递归的方式:

(define (operList op i)
  (lambda (lis)
    (if (null? lis)
        '()
        (cons (op i (car lis))
              ((operList op i) (cdr lis))))))

它按预期工作:

((operList + 2) '(1 1 1))
=> '(3 3 3)

您也可以使用 map:

(define operList
  (lambda (op i)
    (lambda (lst)
      (map
       (lambda (x) (op i x))
       lst))))