Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #lang racket
  2. ; number 11
  3. (define(list-of-nums? lst)
  4. (cond[(empty? lst) '()]
  5. [(=(length lst) 1) (car lst)]
  6. [else(number?(car lst)) (list-of-nums? (cdr lst))]))
  7.  
  8. (define (sorted? lst)
  9. (cond[(empty? lst) '()]
  10. [(=(length lst) 1) (car lst)]
  11. [else(>(car lst) (second lst)) (sorted? (cdr lst))]))
  12.  
  13. (define/contract (sort-descending lst)
  14. (->(and/c list? list-of-nums?) (and/c sorted? list?))
  15. (sort lst (λ(x y) (> x y))))
  16.  
  17. (sort-descending '(1 99 3 2 45 6 -1 5 0 2))
  18.  
  19. ; number 14
  20. (define (sum-elems-divis-by-three lst)
  21. (define (helper acc lst)
  22. (cond[(empty? lst) acc]
  23. [(=(remainder (car lst)3)0) (helper (+ acc(car lst)) (cdr lst))]
  24. [else (helper acc (cdr lst))]))
  25. (helper 0 lst))
  26.  
  27. (sum-elems-divis-by-three '())
  28. (sum-elems-divis-by-three '(1 3 6))
  29. (sum-elems-divis-by-three '(2 3 4 5 6 7 8 9))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement