; A list-of-images is either
; empty
; or (cons n lon),
; where n is an image and lon is a list-of-images.
height-total
画像のリストを消費し、すべての画像の高さの合計を返す関数をどのように開発しますか? 私は混乱しています。これに関数を使用できますimage-height
か?
解決策は、再帰的な手順として自然に従います。これは宿題のように見えるので、詳細を理解させます。もちろん、image-height
手順を使用する必要があります。
(define (height-total list-of-images)
(if <???> ; if the list is empty
<???> ; then what's the height sum of an empty list?
(+ (image-height <???>) ; else add the height of the first image and
(height-total <???>)))) ; advance recursion to the rest of the list
リストに関するこの問題や他の多くの問題の解決策は、同じ基本的な再帰構造に準拠していることに注意してください。この種の問題を解決する方法をよりよく理解するには、プログラムの設計方法またはリトル スキーマのいずれかをよく読むことをお勧めします。
ラケットの使用:
(for/sum ([image image-list])
(image-height image))
また
(foldl (lambda (image sum) (+ (image-height image) sum))
0 image-list)
また
(apply + (map image-height image-list))