schemer5rs

How to Multiply a value to the next value in a list in scheme?


Is there a way to multiply each value in a list, however instead of getting another list (2 4 6) like so, you get the multiplication of all values together.

(define (multi x y)
  (if (null? y)
     '()
     (* x (car y))))
(multi 2 '(1 2 3))
2 * 1 * 2 * 3
==> 6

This may be very simple to some people, but I am having a coding block here, so please don't be rude in the comments. thank you.


Solution

  • (define (mult x lst)
      (if (null? lst)
          x
          (mult (* x (car lst))(cdr lst))))