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

Pages: 1-

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 13:02

BURY YOUR FUCKING DEAD GOYIM!

Name: Anonymous 2012-12-02 13:13

To get you started:

(define (consume lst) (if (null? lst) (display "yum!") (begin (display car lst) (display "..") (consume (cdr lst)))))

Name: Anonymous 2012-12-02 13:28

>>3
Dont you need to define display? Or is it already defined? Using beginning student here.

Name: Anonymous 2012-12-02 13:30

>>4
display is predefined (but you already should know that since you've tried it out already)

Name: Anonymous 2012-12-02 13:41

>>5
Unfortunately we're so restricted to this.

1. We're not allowed to use recursion, we're only allowed to use abstract list methods.
2. We're not allowed to use cdr or car or anything like that. This isn't in the course notes.

Name: Anonymous 2012-12-02 13:46

Here is what you need: [code]reduce filter - + even? odd? lists[code]

Name: Anonymous 2012-12-02 13:55

>>7
did you just list functions I need to use?

Name: Anonymous 2012-12-02 13:58

Boring.
evens-odds[l] :- {
  foldl[{e, acc} -> {odd?[e] ? (acc + e) : (acc - e)}, 0, l]
}

Name: Anonymous 2012-12-02 14:00

Hey Sexy Lady  ( ͡° ͜ʖ ͡°)

Name: Anonymous 2012-12-02 14:37

check my dubs

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.

Name: Anonymous 2012-12-02 16:31

evensOdds xs = (sum . filter odd $ xs) - (sum . filter even $ xs)

Name: Anonymous 2012-12-02 17:10

>>13
dicksgusting

Name: Anonymous 2012-12-02 17:14

((if (odd? x) + -) a x))
Holy fuck, this is why I love Lisp. I'm feeling fuzzy inside.

Name: Anonymous 2012-12-02 17:17

>>12
I like it.

Name: Anonymous 2012-12-02 18:06

>>12
neat

Name: Anonymous 2012-12-02 18:45

foldl (ap (ap . liftM2 if' even . (+)) (-)) 0

Name: Anonymous 2012-12-02 18:47

>>18
Do you even liftM2?

Name: Anonymous 2012-12-02 18:58

>>19
    No instance for (Integral
                       ((a10 -> a20 -> r0) -> m0 a10 -> m0 a20 -> m0 r0))
      arising from a use of `even'
    In the expression: even liftM2

Name: Anonymous 2012-12-02 21:50

[code]evens-odds lst = product (filter even lst) - product (filter odd list)[code]

Name: Anonymous 2012-12-02 21:53

let me try this again
evens-odds lst = sum (filter even lst) - sum (filter odd lst)

Name: Anonymous 2012-12-02 21:56

>>22
I can see you've never actually used Haskell.

Name: Anonymous 2012-12-03 4:12

>>6
all you really need is lambda and if

Name: Anonymous 2012-12-03 4:47

>>24
What's even the point if there's gonna be a point?

Name: Anonymous 2012-12-03 21:38

Racket
Windows
UTF-8

Pick two.

Name: Anonymous 2012-12-04 2:46

>>19
Do you even back->reddit?

Name: Anonymous 2012-12-04 14:20

>>18
foldr (\x a -> (if odd x then (+) else (-)) a x) 0

Name: Anonymous 2012-12-04 17:13

>>28
Pointy functions are training wheels for Lisp kiddies who haven't learned to understand real functional code yet.

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