2009-12-18 34 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

任何這樣的書本都會提供類似的指導,所以它可能不是必需的,因爲這本書可以覆蓋它,或者因爲「地圖」可用。 –

相關問題