This is the text book example where you should use map
, instead of reinventing the wheel:
(define (mul-list lst x)
(map (lambda (n) (* x n)) lst))
But I guess that you want to implement it from scratch. Your code has the following problems:
- You should not call
list
a parameter, that clashes with the built-in procedure of the same name - one that you're currently trying to use!
- The base case should return an empty list, given that we're building a list as output
- We build lists by
cons
ing elements, not by calling list
- You forgot to pass the second parameter to the recursive call of
mul-list
This should fix all the bugs:
(define (mul-list lst x)
(if (null? lst)
'()
(cons (* x (car lst))
(mul-list (cdr lst) x))))
Either way, it works as expected:
(mul-list '(1 2 3) 3)
=> '(3 6 9)