Guest User

Untitled

a guest
Mar 24th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.73 KB | None | 0 0
  1. (define (mode L)
  2.    (mode-helper '() L))
  3.  
  4. (define (mode-helper run L)
  5.  (define (next-run L)
  6.      (if    (and (list? (cdr L)) (equal? (car L) (cadr L)))
  7. ;;the list test is to make sure
  8. ;;cadr will not return an error
  9.         (cons (car L) (next-run (cdr L)))
  10.         (cons (car L) '())))
  11.   (cond ((null? L) run)
  12.     ((> (count (next-run L)) (count run))
  13.      (mode-helper (next-run L) (cdr L)))
  14.     (else (mode-helper run (cdr L)))))
  15. ;;here (cdr L) could be (but-next-run L (next-run L)
  16. ;;return the cdr of both until (next-run L) is null
  17. ;;the make the function more effecient
  18. ;;also you could use a let to avoid calulation (next-run L)
  19. ;;multiple times
  20.  
  21. (define (count L)
  22.     (if (null? L)
  23.     0
  24.     (+ 1 (count (cdr L)))))
  25.  
  26. (mode '(1 2 2 3 3 3 4))
Advertisement
Add Comment
Please, Sign In to add comment