リストから要素を削除することはできますが、Scheme で関数を記述して、リスト内のすべての要素を別の要素に置き換える方法がわかりません。どんな助けでも大歓迎です。
4153 次
1 に答える
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)
(実際にやって学ぶ必要があるので、詳細はお任せします。)
覚えておいてください、Scheme で物事を行う最も自然な方法は、通常、古いリストに少しずつ変更を加えようとするのではなく、古いリストから新しいリストを 1 つずつ組み立てることです。
map
を使用して、これをより簡潔に行うこともできることに注意してください。
于 2009-12-18T03:37:11.787 に答える