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

Pages: 1-

Functional Programming is Gay

Name: Anonymous 2009-01-02 16:50

Yes, this is my homework

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: Anonymous 2009-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: Anonymous 2009-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: Anonymous 2009-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: Anonymous 2009-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: Anonymous 2009-01-02 17:40

If you can't figure this out, then perhaps functional programming is not for you

Name: Anonymous 2009-01-02 17:41

just do it like you would in C then convert it to scheme code it's not too hard

Name: Inevitable Smug Haskell User 2009-01-02 17:45

ghci> let countElem = map (head &&& length) . group . sort in countElem "abcaabcabcacd"
[('a',5),('b',3),('c',4),('d',1)]
ghci>

Name: Anonymous 2009-01-02 17:47

>>6
it isn't, however, it's my homework

Name: Anonymous 2009-01-02 18:06

>>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: Anonymous 2009-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.

Name: Anonymous 2009-01-02 18:20

>>11
"GRUNNER"

Name: Anonymous 2009-01-02 18:27

>>10
turn around
360°?

Name: Anonymous 2009-01-02 18:36

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: Anonymous 2009-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))))))


Duh.

Name: sage 2009-01-03 14:36

Sage.

Name: Anonymous 2009-01-03 23:01

>>1
You obviously haven't read your SICP today.

Name: Anonymous 2009-01-04 4:59

it's actually cool mofo

Name: Anonymous 2009-01-04 5:20

>>1
How the fuck could you have done it in C?  C doesn't have symbolsTerrible!

Name: Anonymous 2009-01-04 5:43

>>19
If you treat the symbols as characters, then it becomes really simple.

Name: Anonymous 2009-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.

Name: Anonymous 2009-01-04 13:49

Read SICP

Name: Anonymous 2009-01-07 4:43

>>23
You are an idiot.

Name: BurningSpace !TvdgKuS4ZM 2009-01-07 5:03

Haha >>23 you sure got told

Name: Anonymous 2009-01-07 5:13

>>23
You'd feel right at home on /pr/. Get back there.

Name: Anonymous 2009-01-07 5:14

>>27
You're telling yourself the truth hurts?

Fuck me your an idiot.

Name: Anonymous 2009-01-07 5:23

>>29
Fuck my your and idiot.
fixed for you

Name: Anonymous 2009-01-07 5:30

>>32
You did right it though!

Dohohohoho

Name: MIT Wizard 2009-01-07 11:17

>>33
is unscientific and ultimately destructive.

Name: Anonymous 2009-01-07 15:45

In newLISP.

> (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: Anonymous 2009-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))

Name: Anonymous 2013-08-05 16:42

(((((((((((((((((((((((((((((Lain

Name: Anonymous 2013-08-05 22:18

...nice bump

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..

Name: Anonymous 2013-08-05 22:29

I bet you can't even write any of those lisp functions in lisp!

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