2009-12-18 35 views

回答

3

一个简单的版本,可以设计这样的:

(define (replace old-element new-element list) 
    ; if the list is null, just return null 
    ; else if the car of the list is equal to old-element 
    ; run replace on the rest of the list, and cons new-element onto it 
    ; else 
    ; run replace on the rest of the list, and cons the car onto it) 

(我离开的细节给你,因为你必须边做边学。)

记住,在方案中最自然的方式来做的事情通常是从你的旧名单中逐一收集一份新的名单,而不是一次一个地修改你的旧名单。

请注意,您也可以使用map更简洁地完成此操作。

+1

这里的语法不好,应该是'(定义(替换旧的新列表)...)'。至于实际的实现 - 不要这样做,使用'map'。 –

+2

我以为他正在从一本指南或教科书中找不到“地图”附近的地方。 – mquander

+0

任何这样的书本都会提供类似的指导,所以它可能不是必需的,因为这本书可以覆盖它,或者因为“地图”可用。 –

相关问题