Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.97 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;lista 11
  4.  
  5. ;; zadanie 1
  6. (define/contract (suffixes xs)
  7.   (parametric->/c [a] (-> (listof a) (listof (listof a))))
  8.                   (if (null? xs) (list xs)
  9.                       (cons xs(suffixes (cdr xs)))))
  10.  
  11. ;; zadanie 2
  12.  
  13. (define/contract (sublists xs)
  14.   (parametric->/c [a] (-> (listof a) (listof (listof a))))
  15.   (if (null? xs)
  16.       (list null)
  17.       (append-map
  18.        (lambda (ys) (list (cons (car xs) ys) ys))
  19.        (sublists (cdr xs)))))
  20.  
  21. ;;  testy
  22. ;;  > (sublists '(1 2))
  23. ;;  '((1 2) 2)
  24. ;;  > (sublists '(1 2 3))
  25. ;;  '((1 2 3) 2 3 (1 . 3) . 3)
  26.  
  27.  
  28. ;; zadanie 3
  29. (define/contract (task3-1 x y)
  30.   (parametric->/c [a b] (-> a b a))
  31.   x)
  32.  
  33. (define/contract (task3-2 x y z)
  34.   (parametric->/c [a b c] (-> (-> a b c) (-> a b) a c))
  35.   (x z)(y z))
  36.  
  37. (define/contract (task3-3 x y)
  38.   (parametric->/c [a b c] (-> (-> b c) (-> a b) (-> a c)))
  39.   (lambda(z) (x (y z))))
  40.  
  41. (define (id x) x)
  42.  
  43. (define/contract (task3-4 f)
  44.   (parametric->/c [a] (-> (-> (-> a a) a) a))
  45.   (f id))
  46.  
  47. ;; >(task3-4 (lambda(x) (x 2)))
  48.  
  49.  
  50. ;; zadanie 4
  51.  
  52. (define/contract (task4-loop x)
  53.   (parametric->/c [a b] (-> a b))
  54.   (task4-loop x))
  55.  
  56. ;; zadanie 5
  57.  
  58. (define/contract (foldl-map f a xs)
  59.   (parametric->/c [a b c]  (->(-> a b (cons/c c b)) b (listof a) (cons/c (listof c)b)))
  60.   (define (it a xs ys)
  61.      (if (null? xs)
  62.           (cons (reverse ys) a)
  63.           (let [(p (f (car xs) a))]
  64.              (it (cdr p)
  65.                   (cdr xs)
  66.                   (cons (car p) ys)))))
  67.   (it a xs null))
  68.  
  69.  
  70. ;;Pierwszy argument powinien być procedura  przyjmujaca dwa argumenty, oznaczajace
  71. ;;(w kolejności) bieżacy element listy oraz bieżacy akumulator, zaś zwracajaca
  72. ;;pare złożona z nowego elementu listy oraz nowej wartości akumulatora.
  73. ;; Pozostałe dwa argumenty powinny zawierać startowa wartość akumulatora
  74. ;;oraz liste elementów do przetworzenia. Procedura foldl-map zwraca pare złożona
  75. ;;z listy wynikowej i końcowej wartości akumulatora.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement