Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created November 25, 2013 18:53
Show Gist options
  • Save yabberyabber/7646579 to your computer and use it in GitHub Desktop.
Save yabberyabber/7646579 to your computer and use it in GitHub Desktop.
;;function lis-length returns the length of a list
;;list -> number
(define (lis-length lis)
(cond [(empty? lis) 0]
[else (add1 (lis-length (rest lis)))]))
(check-expect (lis-length (list 1 2 3 4 5)) 5)
(check-expect (lis-length empty) 0)
(check-expect (lis-length (list 1)) 1)
;;function mean computes the mean of a list of numbers
;;list-of-numbers -> number
(define (mean lis)
(cond [(empty? (rest lis)) (first lis)]
[else (/ (+ (* (mean (rest lis))
(lis-length (rest lis)))
(first lis))
(lis-length lis))]))
(check-expect (mean (list 0 2))
1)
(check-expect (mean (list 10 10 10 10 0))
8)
(check-expect (mean (list 8))
8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment