Define a Scheme function (count-elm L), that takes a list and returns a list with each element and its number of occurrences. For example:
>(count-elm '(a b c a a b c a b c a c d))
((a 5) (b 3) (c 4) (d 1))
I could have done this in seconds in C or something - what the fuck
Name:
Anonymous2009-01-02 16:52
If you do that in seconds in C, it will come back and bite you in the ass. Please don't do that in seconds in C. Thank you.
Name:
Anonymous2009-01-02 16:59
I could do it in a few months in JAVA, ENTERPRISE style, and there is the time for documentation
Regardless, this should be simple enough - how do I do it in Scheme?
Name:
Anonymous2009-01-02 17:03
I can think of ways to accomplish this in 10-12 other languages easily.
Surley you can figure this out for Scheme.
Name:
Anonymous2009-01-02 17:21
>>4
I could do something similar HOWEVER NONE OF THEM ARE FUNCTIONAL LANGUAGES JESUS WERE THEY TRYING TO BE SARCASTIC WHEN THEY CALLED IT "FUNCTIONAL"?
Name:
Anonymous2009-01-02 17:40
If you can't figure this out, then perhaps functional programming is not for you
Name:
Anonymous2009-01-02 17:41
just do it like you would in C then convert it to scheme code it's not too hard
>>8
Write this on a notecard. Be the last one into class, walking in once everyone else is situated. Put it in the professor's desk, turn around, and walk away.
Name:
Anonymous2009-01-02 18:15
>>1
My Scheme is awfully rusty (and by that, I mean I've never used it), but couldn't you use an associative list to store the return value? Use the list item as the key (in this case, a, b, c, etc.) and increase the value of each of those when you come across an instance of it.
This is not a hard problem. It's a little more work if you aren't allowed to rely on whatever suitable data structure your system provides, but it's not hard. Just write the program.
Name:
Anonymous2009-01-02 19:56
(define (inc-elem x in)
(cond ((null? in) (list (cons x 1)))
((eq? x (caar in)) (cons (cons x (+ 1 (cdar in))) (cdr in)))
(else (cons (car in) (inc-elem x (cdr in))))))
>>1
How the fuck could you have done it in C? C doesn't have symbols! Terrible!
Name:
Anonymous2009-01-04 5:43
>>19
If you treat the symbols as characters, then it becomes really simple.
Name:
Anonymous2009-01-04 9:42
Functional programming is a good fit for some problems, but can be cumbersome or inefficient for others. Any good course will of course assign you both kinds, to give you valuable practical experience that can aid you in choosing the right tools for solving problems you'll encounter later.
Now try that again, but this time in parallel, using threads.
> (define (count-elm L) (let (uL (unique L)) (map cons uL (count uL L)) ))
(lambda (L)
(let (uL (unique L))
(map cons uL (count uL L)))) (count-elm '(a b c a a b c a b c a c d))
((a 5) (b 3) (c 4) (d 1))
Name:
Anonymous2009-01-07 15:56
Alternative > (define (count-elm L) (map (fn (x) (cons x (count (list x) L))) (unique L)))
(lambda (L) (map (lambda (x) (cons x (count (list x) L))) (unique L)))
> (count-elm '(a b c a a b c a b c a c d))
((a 5) (b 3) (c 4) (d 1))
ugh, why is one line of lisp like ten lines of lisp...
and what's with all the higher-level (cheat) function calls..
I bet one could do it in C without a single library / built-in call..