Skip to content

Instantly share code, notes, and snippets.

@shirok
Created June 6, 2011 09:59
Show Gist options
  • Save shirok/1010022 to your computer and use it in GitHub Desktop.
Save shirok/1010022 to your computer and use it in GitHub Desktop.
(define (count xs)
(if (or (null? xs)
(null? (cdr xs)))
0
(let ([minimum (apply min xs)])
(cond
[(= minimum (first xs)) (count (cdr xs))]
[(= minimum (last xs)) (+ 1 (count (cdr (reverse xs))))]
[else (let loop ([L '()] [R xs])
(if (= minimum (car R))
(+ 2 (count (append (cdr R) L)))
(loop (cons (car R) L) (cdr R))))]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment