Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Scheme/DrRacket

Name: Anonymous 2012-12-02 12:58

How do you do this shit /prog/?

Write a Scheme function evens-odds (using at least one abstract list function), which consumes a list of numbers lst and produces the difference between the sum of the odd numbers and the sum of the even numbers in the list lst. Some examples follow:


(evens-odds (list 1 2 3 4)) => -2
(evens-odds empty) => 0
(evens-odds (list 2 4 16)) => -22
(evens-odds (list 1 3 7 21 13)) => 45



Note: You should use local function(s) only (in case of need).

Name: Anonymous 2012-12-02 15:37

Be cool, and write something like this instead of filtering/reducing twice:

(define (evens-odds l)
  (define (f x a) ((if (odd? x) + -) a x))
  (foldl f 0 l))


I'm not going to explain my code because that would be HELPING HIM.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List