For the simplest solution, use apply
for this:
(define (multiply-all lst)
(apply * lst))
If you need to build the procedure from scratch, just remember that the base case (an empty list) should return 1
, and the recursive step should multiply the current value using the standard solution template, like this:
(define (multiply-all lst)
(if (empty? lst)
1
(* (first lst)
(multiply-all (rest lst)))))
For a nicer answer, you can try using tail recursion:
(define (multiply-all lst)
(let loop ([lst lst] [acc 1])
(if (empty? lst)
acc
(loop (rest lst) (* (first lst) acc)))))
Anyway the procedures work as expected:
(multiply-all '())
=> 1
(multiply-all '(3 5 4))
=> 60
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…