这是家庭作业,所以我不想要答案。我只需要朝着正确的方向推动。我需要将多个函数映射到列表中。例如:

(map-multi (list plus-one square) '(4 5 6)) => (25 36 49)

我能够让它将第一个函数映射到列表的元素,但是,之后我会非常迷失。此外,由于这是介绍性的,我仅限于介绍性功能(const, append, car, cdr, member, 等。)

(define (map-multi f l)  
    (cond  
        ((null? l)  
            l)  
        (else (cons ((car f) (car l))  
            (map-multi f (cdr l))))))  
有帮助吗?

解决方案

你需要 作曲 你在 f 参数。为了简单起见,假设列表中只有两个函数-那么您需要将第一个函数应用于数字列表中的当前元素,然后将第二个函数应用于结果。如果您可以使用 compose 过程继续进行并在代码中更改此行:

((car f) (car l)) ; you're applying only the 1st function! what about the 2nd?

...用这个:

((compose (cadr f) (car f)) (car l))       ; now we're applying both functions

如果你不能使用 compose, ,然后用这个替换同一行:

((cadr f) ((car f) (car l)))               ; now we're applying both functions

现在,如果问题更普遍,并且需要将函数列表映射为 两个以上 元素,然后再次用这个替换代码中的同一行:

((compose-multi f) (car l))

并实现一个帮助函数,该函数通过连续调用 compose.这一个是留给你的练习,因为它是家庭作业-但如果你理解上面的代码是如何工作的两个函数,它应该很容易扩展多个函数列表的结果:

(define (compose-multi flist)      ; procedure for composing a list of functions
  (if (null? flist)                ; if the list is empty then
      <???>                        ; return the identity function
      (<???> (compose-multi <???>) ; else compose the result of recursive call
             <???>)))              ; with the current element in the list

请注意,identity函数是处理函数列表中没有元素的情况所必需的;定义非常简单,它只是返回作为参数传递的相同值。

还要注意 compose-multi 返回a 功能, ,组成列表中所有函数的结果 - compose 这样做是为了你,但如果你不允许使用它,请记住这一点:

(compose x y)

...相当于这个:

(lambda (n) (x (y n)))

其他提示

将其写为两个函数可能会更容易。一个函数列表和一个输入,并应用列表中的所有函数串联。一个函数应用程序的输出将是下一个应用程序的输入;一旦你用完了功能,你就完成了。

另一个函数将简单地将此帮助函数映射到一个输入列表中。

这是定义的另一种方法 multi-map 而不是组合使用的操作称为 fold.由于您只允许使用介绍性函数,因此这不是您作业的真正答案。但它会,如果你写你自己的定义 fold (不是很长!)

(define (multi-map operations input)
  (fold map input operations))

> (multi-map (list 1+ square)
             '(4 10 8))
$2 = (25 121 81)

> (multi-map (list 1+ square 1+) 
             '(4 10 8))
$3 = (26 122 82)

要获得温暖,从一个更简单的问题开始。然后概括解决方案。

你会怎么写这个函数?

(define (map-single fs x)
  ...)

> (map-single (list double add1) 3)
7

这需要一个列表, fs, ,函数值作为参数和数字, x, ,并计算了在 fsx?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top