So I encountered a problem where I have a list of numbers that I need to multiply out and return that value. However, if the list does not contain numbers or is an empty list, return false. Please note that I cannot use any built in functions and would like to understand from scratch in Scheme R5RS. (Example: make-list is a function in Scheme, but can be done recursively without using the function itself)
I implemented a helper function where the base case is if the list is empty, return false. Otherwise, recursively multiply inside the list. However, this doesn't work since the if condition is returning a boolean whereas the recursive method is returning a function.
Also, I have tried returning my base case into a value which is good except for when I am testing an empty list which is suppose to be #f but a value is returned instead.
An example would be:
(mult-this '(2 3 4)) -> 24
(mult-this '(1 2 a b)) -> #f
(mult-this '()) -> #f
Here is my psuedo-code (helper)
(define multiply-this (lambda (mult)
(if (null? mult)
#f
(* recursively multiply car mult and cdr mult)
One pure-R5RS way:
Basically, it first checks for an empty list, and then for the non-empty case, it recursively iterates over the list using a named let, multiplying together numbers as it goes, and returning early with
#fif it encounters a non-number value before the end of the list.Example usage: