
Untitled
By: a guest on
Mar 24th, 2012 | syntax:
Scheme | size: 0.73 KB | hits: 46 | expires: Never
(define (mode L)
(mode-helper '() L))
(define (mode-helper run L)
(define (next-run L)
(if (and (list? (cdr L)) (equal? (car L) (cadr L)))
;;the list test is to make sure
;;cadr will not return an error
(cons (car L) (next-run (cdr L)))
(cons (car L) '())))
(cond ((null? L) run)
((> (count (next-run L)) (count run))
(mode-helper (next-run L) (cdr L)))
(else (mode-helper run (cdr L)))))
;;here (cdr L) could be (but-next-run L (next-run L)
;;return the cdr of both until (next-run L) is null
;;the make the function more effecient
;;also you could use a let to avoid calulation (next-run L)
;;multiple times
(define (count L)
(if (null? L)
0
(+ 1 (count (cdr L)))))
(mode '(1 2 2 3 3 3 4))