troubleshooting Question

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

Avatar of speedingcars2012
speedingcars2012 asked on
Programming Languages-OtherDatabasesProgrammingPuzzles / RiddlesMiscellaneous
11 Comments1 Solution1107 ViewsLast Modified:
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")
ASKER CERTIFIED SOLUTION
harfang

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros