Question: I am programming in Scheme, I am trying to make a function where you take a list of numbers, reverse the list, throw out the

I am programming in Scheme, I am trying to make a function where you take a list of numbers, reverse the list, throw out the negative numbers and summantion the non-negatives. Here is my code:
(define (sum n)
(if (eq?n 0)
0
(+n (sum (-n 1)))))
(define (ReverseEm elems)
(if (null?elems)
'()
(append (ReverseEm (cdr elems))
(list (car elems)))))
(define (nonNegative num)
(if (>=num 0)
num
0))
(define (JustDoIt lst)
(if (nonNegative (ReverseEm (sum lst))))
If I run (JustDoIt '(3-1460205))it says ill-formed special form: if
The correct output should be (30216)
Because summation of 3is 1+2+3=6,-14gets thrown out, 1+2+3+4+5+6=21,0=0,1+2=3,-5gets thrown out, the list gets: reversed: (30216),I feel I am close, but I can't get the form right in scheme because I am unfimiliar with it.Please understand that I don't want to sum all the non-negatives together, I want their SUMMATION, big difference, I explain it in great detail. I would also like to add that I only want to talk about scheme, not c++,or python, or Algol 60,or Plankalkul, or intercal, or any other programming langauge, except for scheme, a dialect of Lisp. We are only to talk in terms of scheme. Another thing I would like to add is that I tested the three functions I made, they work as intended. ReverseEm reverses a list. nonNegative spits back the number at you, unless it's negative, it spits back a zero. And the sum functions works as intended. I am having trouble with the last part of the code. How do I fix (define (JustDoIt lst)
(if (nonNegative (ReverseEm (sum lst)))),so it returns (30216), and I want to make it really clear that I do not want to output 16, I want to output (30216), how do I fix JustDoIt?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!