Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 24th, 2012  |  syntax: Scheme  |  size: 0.73 KB  |  hits: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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))