I am basically new to Scheme, and this is my second attempt to solve a certain problem.
So what I am trying to do basically is sum each parameter we pass into the function, with an appropriate value for example:
(sum 3 1 2 0 2) ;would return 228
Here is my code:
(define (sum one five ten twenty fifty hundred)
(+ (* 1 one)(* 5 five) (* 10 ten) (* twenty 20) (* 50 fifty) (* 100 hundred))
I think a possible solution is using the lambda function, but I could't know how to implement it.
Best How To :
Here is one way you can calculate the numbers by sending in only 5 arguments instead of 6:
(lambda (L skip) ;stores yourList and the number you want to skip
(lambda (L L2 total) ;yourList, (1,5,10...) list, running total
((null? L) (list total)) ;print the total when finish yourList
;if the next number is the skip number, just move on to next value
((= (car L2) skip) (loop L (cdr L2) total))
;otherwise, increase total and send in both lists, minus the head, to the loop
(else (loop (cdr L) (cdr L2) (+ total (* (car L) (car L2)) )))
)(loop L (list 1 5 10 20 50 100) 0) ;initial values of loop
;send in your list of numbers, plus the value place you want to skip
(sum (list 3 1 2 0 2) 20) ; ==> 228
I would be much easier, though, to just fill in all the places that you don't want with a 0. Once you have 6 arguments, the following code will work.
(lambda (one five ten twenty fifty hundred)
(+ (* 1 one) (* 5 five) (* 10 ten) (* 20 twenty) (* 50 fifty) (* 100 hundred) )
(sum 3 1 2 0 0 2)