Advertisement
Guest User

fucking scholar

a guest
Nov 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1.  
  2. ;; e)
  3.  
  4. ;; (nheight nlist) consumes a nested list nlist and returns
  5. ;; the height corresponding to the maximum number of lists that
  6. ;; have been nested
  7. ;; nheight: Nested-Listof-Any -> Nat
  8. ;; Examples:
  9. (check-expect (nheight empty) 1)
  10. (check-expect (nheight '((1))) 2)
  11.  
  12. (define (nheight nlist)
  13. (nfoldr (lambda (x y) y)
  14. (lambda (x y) (max (+ x 1) y))
  15. 1
  16. nlist))
  17.  
  18. ;; Test:
  19. (check-expect (nheight '((1) 2 ((3)))) 3)
  20.  
  21.  
  22. ;; f)
  23.  
  24. ;; (prune nlist) consumes a nested list nlist and returns a
  25. ;; list where all empty nested lists have been removed
  26. ;; prune: Nested-Listof-Num -> Nest-Listof-Num
  27. ;; Examples:
  28. (check-expect (prune '()) empty)
  29. (check-expect (prune '(1 () 2)) '(1 2))
  30.  
  31. (define (prune nlist)
  32. (nfoldr (lambda (x y) (cons x y))
  33. (lambda (x y)
  34. (cond
  35. [(empty? x) (append y)]))
  36. empty
  37. nlist))
  38.  
  39. ;; Tests:
  40. (check-expect (prune '(1 2 3)) '(1 2 3))
  41. (check-expect (prune '(1 () 3 () (()))) '(1 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement