Question

Previously I had the question about adding a character to a frequency list(Add a character to a frequency list), it got solved, but i have problems again with the rest of the project. The next 2 functions are working:

Write a function which creates the frequency list( from a list of characters)

(statistiques '("a" "b" "r" "a" "c" "a" "d" "a" "b" "r" "a")) 
 → (("a" 5) ("r" 2) ("b" 2) ("d" 1) ("c" 1)) 

My code:

(define statistiques 
  (lambda (l)
    (if (null? l)
        l
        (ajoute1(car l)(statistiques (cdr l))))))

Write a function which is inserting a pair (a character and a number which indicates the occurrence of that character in a list), in a list of pairs which is sorted by the number of occurrence

(inserefreq '("b" 2) '(("d" 1) ("a" 5))) 
 → (("d" 1) ("b" 2) ("a" 5))

(define inserefreq
  (lambda (c l)
    (cond ((null? l) 
           (list c))
      ((<= (cadr c) (cadar l))
           (cons c l))
          (else
           (cons (car l) (inserefreq c (cdr l)))))))*

Then the problem is with the next one, which is asking to sort a frequency list by successive insertion

(triefreq '(("a" 5) ("r" 2) ("b" 2) ("d" 1) ("c" 1))) 
 → (("d" 1) ("c" 1) ("r" 2) ("b" 2) ("a" 5))

My code:

(define tirefreq
  (lambda (l)
    (inserefreq(car l) (tirefreq (cdr l)))))

Result/error: error

Était-ce utile?

La solution

You're just missing the base case, when l is empty:

(define triefreq
  (lambda (l)
    (if (null? l)
        '()
        (inserefreq (car l) (triefreq (cdr l))))))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top