Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. ;;contacty to dynamiczny system typów, własności są sprawdzane dopiero przy konieczności
  2. ;;system typów w rackecie typowanym to system statyczny- typy sprawdzane po napisaniu przed
  3. ;;wywołaniem.
  4.  
  5. (require racket/contract)
  6.  
  7. ;;zadanie 1
  8.  
  9. (define/contract (suffixes xs)
  10. (let (( a (new-∀/c 'a)))
  11. (-> (listof a) (listof (listof a))))
  12. (if (null? xs) (list null)
  13. (cons xs (suffixes (cdr xs)))))
  14.  
  15. ;;zadanie 2
  16.  
  17. (define (dist x y)
  18. (abs (- x y)))
  19.  
  20. (define (average x y)
  21. (/ (+ x y) 2))
  22.  
  23. (define (square x)
  24. (* x x))
  25.  
  26. (define/contract (sqrt x)
  27. (->i ([argument (>/c 0)])
  28. [result (argument) (and/c (λ (result) (if (>= argument 1) (< result argument)
  29. (> result argument)))
  30. (λ (result) (> result 0)))])
  31. ;; lokalne definicje
  32. ;; poprawienie przybliżenia pierwiastka z x
  33. (define (improve approx)
  34. (average (/ x approx) approx))
  35. ;; nazwy predykatów zwyczajowo kończymy znakiem zapytania
  36. (define (good-enough? approx)
  37. (< (dist x (square approx)) 0.0001))
  38. ;; główna procedura znajdująca rozwiązanie
  39. (define (iter approx)
  40. (cond
  41. [(good-enough? approx) approx]
  42. [else (iter (improve approx))]))
  43.  
  44. (iter 1.0))
  45.  
  46. ;;zadanie 3
  47.  
  48. (define/contract (filter pred seq)
  49. (let ((a (new-∀/c 'a)))
  50. (-> (-> a boolean?) (listof a) (listof a)))
  51. (cond [(null? seq) null]
  52. [(pred (car seq)) (cons (car seq) (filter pred (cdr seq)))]
  53. [else (filter pred (cdr seq))]))
  54.  
  55. (define/contract (filter2 pred seq)
  56. (and/c (let ((a (new-∀/c 'a)))
  57. (-> (-> a boolean?) (listof a) (listof a)))
  58. (->i ([pred (-> any/c boolean?)]
  59. [seq list?])
  60. [result (seq pred) (λ (result) (<= (length result) (length seq)))]))
  61.  
  62. (cond [(null? seq) null]
  63. [(pred (car seq)) (cons (car seq) (filter pred (cdr seq)))]
  64. [else (filter pred (cdr seq))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement