Scheme/DrRacket
1
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).
2
Name:
Anonymous
2012-12-02 13:02
BURY YOUR FUCKING DEAD GOYIM!
3
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)))))
4
Name:
Anonymous
2012-12-02 13:28
>>3
Dont you need to define display? Or is it already defined? Using beginning student here.
5
Name:
Anonymous
2012-12-02 13:30
>>4
display is predefined (but you already should know that since you've tried it out already)
6
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.
7
Name:
Anonymous
2012-12-02 13:46
Here is what you need: [code] reduce filter - + even? odd? lists[code]
8
Name:
Anonymous
2012-12-02 13:55
>>7
did you just list functions I need to use?
9
Name:
Anonymous
2012-12-02 13:58
Boring.
evens-odds[l] :- {
foldl[{e, acc} -> {odd?[e] ? (acc + e) : (acc - e)}, 0, l]
}
10
Name:
Anonymous
2012-12-02 14:00
Hey Sexy Lady ( ͡° ͜ʖ ͡°)
11
Name:
Anonymous
2012-12-02 14:37
check my dubs
12
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 .
13
Name:
Anonymous
2012-12-02 16:31
evensOdds xs = (sum . filter odd $ xs) - (sum . filter even $ xs)
14
Name:
Anonymous
2012-12-02 17:10
15
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.
16
Name:
Anonymous
2012-12-02 17:17
17
Name:
Anonymous
2012-12-02 18:06
18
Name:
Anonymous
2012-12-02 18:45
foldl (ap (ap . liftM2 if' even . (+)) (-)) 0
19
Name:
Anonymous
2012-12-02 18:47
20
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
21
Name:
Anonymous
2012-12-02 21:50
[code] evens-odds lst = product (filter even lst) - product (filter odd list)[code]
22
Name:
Anonymous
2012-12-02 21:53
let me try this again
evens-odds lst = sum (filter even lst) - sum (filter odd lst)
23
Name:
Anonymous
2012-12-02 21:56
>>22
I can see you've never actually used Haskell.
24
Name:
Anonymous
2012-12-03 4:12
>>6
all you really need is lambda and if
25
Name:
Anonymous
2012-12-03 4:47
>>24
What's even the point if there's gonna be a point?
26
Name:
Anonymous
2012-12-03 21:38
Racket
Windows
UTF-8
Pick two.
27
Name:
Anonymous
2012-12-04 2:46
>>19
Do you even
back->reddit?
28
Name:
Anonymous
2012-12-04 14:20
>>18
foldr (\x a -> (if odd x then (+) else (-)) a x) 0
29
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.