Link to home
Start Free TrialLog in
Avatar of speedingcars2012
speedingcars2012

asked on

I need a function in scheme that counts the frequency of a character

I'm making a hufftree in scheme and I need a function count-freq that takes a string representing a message and creates a list of charfreq, or a list of the frequency of each character in the list.

I'm using the compiler Dr. scheme with the language intermediate student, if that's relevant. it's available at
http://download.plt-scheme.org/
;;example
(count-freq "she sells seashells")
(list
(make-charfreq #\s 6)
(make-charfreq #\h 2)
(make-charfreq #\e 4)
(make-charfreq #\space 2)
(make-charfreq #\l 4)
(make-charfreq #\a 1))
 
;;so far I've only been able to come up with this-  it's wrong though and the recursion is messed up
 
;;(make-charfreq #\t 4)
(define-struct charfreq (ch freq)) 
 
;;count-freq : string -> list
;;takes a string representing a message and produces a list of the frequency each character in the string appears
;;(define (count-freq string)...)
;;Example: (count-freq "she sells seashells") should produce
;;(list
;;(make-charfreq #\s 6)
;;(make-charfreq #\h 2)
;;(make-charfreq #\e 4)
;;(make-charfreq #\space 2)
;;(make-charfreq #\l 4)
;;(make-charfreq #\a 1))
 
(define (count-freq string)
  (cond
    [(empty? (string->list string)) '()]
    [(insert-char (first (string->list string)) (string->list string))]
    [else (insert-char char (rest (list->string string)))]))
 
(define (insert-char char string)
  (cond
  [(empty? (char-freq char)) (= (char-freq char) 1)]
    [(char=? (first (string->list string)) (second (string->list string))) 
            (+ 1 (charfreq-freq char))])) 
    
(count-freq "she sells seashells")

Open in new window

Avatar of speedingcars2012
speedingcars2012

ASKER

a reference for scheme is available online at
http://www.htdp.org/2003-09-26/
and info about plt scheme in general
http://plt-scheme.org/
Avatar of DanRollins
Sorry... I have no idea about that program (Dr. Scheme).  
However, if you want help with a C++ program to do this, let me know :-)
No, I asked it on Wednesday a little after noon (3 days ago). One of the site administrators told me Scheme isn't well-known on this site. I just didn't see the point in letting it linger if no one knows how to answer it. Since it says it won't be deleted until 4 days after I request to delete it, then I would think it's reasonable to believe if no one has answered after a week, then no one knows how to answer. Am I being unreasonable?
Sorry.  Misread the date or read the date on the wrong post.

Not really unreasonable.

I would help but I don't know Scheme.  Is there a free Scheme interpreter?

mlmcc
This is a link to Scheme interpreters:
http://elvis.rowan.edu/~nlt/interpreters.html

All of them appear to be free, or at least the ones open to the general public.  I'm using Dr. Scheme, which is free and works on PC, Mac, or Linux. I called Dr. Scheme a compiler in my question, upon further research it seems that it's actually an interpreter?  Interesting.
ASKER CERTIFIED SOLUTION
Avatar of Markus Fischer
Markus Fischer
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yay someone figured it out. I didn't think anyone knew how. It was a lot harder than the other functions. Or I thought so, anyway. Thanks again!
Thanks!

Yes, it was a bit harder than the others. In my experience, Scheme training is always quite steep, going from variables and expressions to complex structures in a matter of days...

Keep it up!
(°v°)
Thanks! And you haven't seen {http:/Q_24859510.html#25713233}, the ultimate goal of these Scheme questions -- a full Huffman tree generator.
I'm always having fun with Scheme, too much perhaps.
(^v°)
Thanks.  I only saw this one when help was asked for and out of interest monitored it.

mlmcc
mlmcc, I see that you were quite ready to download a Scheme interpreter just to answer this question. That deserves credit! And it's almost a shame that I answered, because, if you have never played with any Lisp spin-off, it's a ride into "Zen, and the art of algorithm maintenance"...

(°v°)