Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. "problem 1."
  2. (define (first s) (car s))
  3. (define (rest s) (cdr s))
  4.  
  5. (define (merge-streams s t)
  6. (if (or (null? s) (null? t))
  7. '()
  8. (cons (first s) (lambda () (merge-streams (rest s) t)))))
  9.  
  10. "problem 2."
  11. (define (merge-compress s t)
  12. (cond ((< (first s) (first t)) (cons (first s) (lambda () (merge-compress (rest s) t))))
  13. ((> (first s) (first t)) (cons (first t) (lambda () (merge-compress s (rest t)))))
  14. ((= (first s) (first t)) (cons (first s) (lambda () (merge-compress (rest s) (rest t)))))))
  15.  
  16. "problem 3a."
  17. (define (c-add pair pair2)
  18. (cons (+ (car pair) (car pair2)) (+ (cdr pair) (cdr pair2))))
  19.  
  20. (define (c-multiply pair pair2)
  21. (cons (- (* (car pair) (car pair2)) (* (cdr pair) (cdr pair2)))
  22. (+ (* (car pair) (cdr pair2)) (* (cdr pair) (car pair2)))))
  23.  
  24. (define (c-length pair)
  25. (sqrt (+ (* (car pair) (car pair)) (* (cdr pair) (cdr pair)))))
  26.  
  27. "problem 3b."
  28. (define (mandelbrot-sequence-trail z a)
  29. (cons a (lambda () (mandelbrot-sequence-trail z (c-add z (c-multiply a a))))))
  30.  
  31. (define (mandelbrot-sequence z)
  32. (mandelbrot-sequence-trail z (cons 0 0)))
  33.  
  34. "problem 3c."
  35. (define (s-map f s)
  36. (if (null? s)
  37. s
  38. (cons (f (car s))
  39. (lambda () (s-map f (rest s))))))
  40.  
  41. (define (mandelbrot-sequence-length z)
  42. (s-map c-length (mandelbrot-sequence z)))
  43.  
  44. "problem 4."
  45. (define (h-min H) (car H))
  46. (define (left H) (cadr H))
  47. (define (right H) (caddr H))
  48.  
  49. (define (combine-heaps H1 H2)
  50. (cond ((null? H1) H2)
  51. ((null? H2) H1)
  52. ((< (h-min H1) (h-min H2))
  53. (create-heap (h-min H1)
  54. H2
  55. (combine-heaps (left H1) (right H1))))
  56. (else (create-heap (h-min H2)
  57. H1
  58. (combine-heaps (left H2) (right H2))))))
  59.  
  60. (define (remove-minimum H)
  61. (combine-heaps (left H) (right H)))
  62.  
  63. (define (sorted-stream-h H)
  64. (cons (h-min H) (lambda () (sorted-stream-h (remove-minimum H)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement