Advertisement
Guest User

Untitled

a guest
May 10th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 7.54 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;; pomocnicza funkcja dla list tagowanych o określonej długości
  4.  
  5. (define (tagged-tuple? tag len p)
  6.   (and (list? p)
  7.        (= (length p) len)
  8.        (eq? (car p) tag)))
  9.  
  10. (define (tagged-list? tag p)
  11.   (and (pair? p)
  12.        (eq? (car p) tag)
  13.        (list? (cdr p))))
  14.  
  15. ;;
  16. ;; WHILE
  17. ;;
  18.  
  19. ; memory
  20.  
  21. (define empty-mem
  22.   null)
  23.  
  24. (define (set-mem x v m)
  25.   (cond [(null? m)
  26.          (list (cons x v))]
  27.         [(eq? x (caar m))
  28.          (cons (cons x v) (cdr m))]
  29.         [else
  30.          (cons (car m) (set-mem x v (cdr m)))]))
  31.  
  32. (define (get-mem x m)
  33.   (cond [(null? m) 0]
  34.         [(eq? x (caar m)) (cdar m)]
  35.         [else (get-mem x (cdr m))]))
  36.  
  37. ; arith and bool expressions: syntax and semantics
  38.  
  39. (define (const? t)
  40.   (number? t))
  41.  
  42. (define (true? t)
  43.   (eq? t 'true))
  44.  
  45. (define (false? t)
  46.   (eq? t 'false))
  47.  
  48. (define (op? t)
  49.   (and (list? t)
  50.        (member (car t) '(+ - * / = > >= < <= not and or mod expt))))
  51.  
  52. (define (op-op e)
  53.   (car e))
  54.  
  55. (define (op-args e)
  56.   (cdr e))
  57.  
  58. (define (op->proc op)
  59.   (cond [(eq? op '+) +]
  60.         [(eq? op '*) *]
  61.         [(eq? op '-) -]
  62.         [(eq? op '/) /]
  63.         [(eq? op '=) =]
  64.         [(eq? op '>) >]
  65.         [(eq? op '>=) >=]
  66.         [(eq? op '<)  <]
  67.         [(eq? op '<=) <=]
  68.         [(eq? op 'not) not]
  69.         [(eq? op 'and) (lambda x (andmap identity x))]
  70.         [(eq? op 'or) (lambda x (ormap identity x))]
  71.         [(eq? op 'mod) modulo]
  72.         [(eq? op 'expt) expt]))
  73.  
  74. (define (var? t)
  75.   (symbol? t))
  76.  
  77. (define (eval-arith e m)
  78.   (cond [(true? e) true]
  79.         [(false? e) false]
  80.         [(var? e) (get-mem e m)]
  81.         [(op? e)
  82.          (apply
  83.           (op->proc (op-op e))
  84.           (map (lambda (x) (eval-arith x m))
  85.                (op-args e)))]
  86.         [(const? e) e]))
  87.  
  88. ;; syntax of commands
  89.  
  90. (define (assign? t)
  91.   (and (list? t)
  92.        (= (length t) 3)
  93.        (eq? (second t) ':=)))
  94.  
  95. (define (assign-var e)
  96.   (first e))
  97.  
  98. (define (assign-expr e)
  99.   (third e))
  100.  
  101. (define (if? t)
  102.   (tagged-tuple? 'if 4 t))
  103.  
  104. (define (if-cond e)
  105.   (second e))
  106.  
  107. (define (if-then e)
  108.   (third e))
  109.  
  110. (define (if-else e)
  111.   (fourth e))
  112.  
  113. (define (while? t)
  114.   (tagged-tuple? 'while 3 t))
  115.  
  116. (define (while-cond t)
  117.   (second t))
  118.  
  119. (define (while-expr t)
  120.   (third t))
  121.  
  122. (define (block? t)
  123.   (list? t))
  124.  
  125. ;; state
  126.  
  127. (define (res v s)
  128.   (cons v s))
  129.  
  130. (define (res-val r)
  131.   (car r))
  132.  
  133. (define (res-state r)
  134.   (cdr r))
  135.  
  136. ;; psedo-random generator
  137.  
  138. (define initial-seed
  139.   123456789)
  140.  
  141. (define (rand max)
  142.   (lambda (i)
  143.     (let ([v (modulo (+ (* 1103515245 i) 12345) (expt 2 32))])
  144.       (res (modulo v max) v))))
  145.  
  146. ;; WHILE interpreter
  147.  
  148. (define (old-eval e m)
  149.   (cond [(assign? e)
  150.          (set-mem
  151.           (assign-var e)
  152.           (eval-arith (assign-expr e) m)
  153.           m)]
  154.         [(if? e)
  155.          (if (eval-arith (if-cond e) m)
  156.              (old-eval (if-then e) m)
  157.              (old-eval (if-else e) m))]
  158.         [(while? e)
  159.          (if (eval-arith (while-cond e) m)
  160.              (old-eval e (old-eval (while-expr e) m))
  161.              m)]
  162.         [(block? e)
  163.          (if (null? e)
  164.              m
  165.              (old-eval (cdr e) (old-eval (car e) m)))]))
  166. ;; ZAD B
  167.  
  168. (define (my-res val mem) ;;moja abstrakcja danych do eval-arith-n
  169.   (cons val mem))
  170. (define (my-val e)
  171.   (car e))
  172. (define (my-mem e)
  173.   (cdr e))
  174.  
  175. (define (eval e m seed) ;;seed bedzie w memory
  176.   (define (eval-h e m) ;;działa podobnie do old z wyjątkiem takim że po wykonaniu eval-arith-n
  177.   (cond [(assign? e)  ;;zwróci pare wartość i pamięć w której może być zmodyfkiowana zmienna seed
  178.          (let ((expr (eval-arith-n (assign-expr e) m)))
  179.            (set-mem
  180.             (assign-var e)
  181.             (my-val expr)
  182.             (my-mem expr)))]
  183.         [(if? e)
  184.          (let ((if-cond-res (eval-arith-n (if-cond e) m)))
  185.          (if (my-val if-cond-res)
  186.              (eval-h (if-then e) (my-mem if-cond-res))
  187.              (eval-h (if-else e) (my-mem if-cond-res))))]
  188.         [(while? e)
  189.          (let ((while-cond-res (eval-arith-n (while-cond e) m)))
  190.            (if (my-val while-cond-res)
  191.                (eval-h e (eval-h (while-expr e) (my-mem while-cond-res)))
  192.                (my-mem while-cond-res)))]
  193.         [(block? e)
  194.          (if (null? e)
  195.              m
  196.              (eval-h (cdr e) (eval-h (car e) m)))]))
  197.   (eval-h e (set-mem 'seed seed m)))
  198.  
  199. (define (rand? e) ;;predykat do rand
  200.   (and (list? e)
  201.        (= (length e) 2)
  202.        (eq? (car e) 'rand)))
  203.  
  204. (define (eval-arith-n e m) ;;teraz zwróci wartość i memory
  205.   (cond [(true? e) (my-res true m)]
  206.         [(false? e) (my-res false m)]
  207.         [(var? e) (my-res (get-mem e m) m)]
  208.         [(rand? e) ((lambda (max) (let* ([max-res (eval-arith-n max m)] ;;najpierw wyliczy max
  209.                                        [res ((rand (my-val max-res)) (get-mem 'seed (my-mem max-res)))]) ;;potem robi rand na seedzie z memory wyciagniętego z policzonego max
  210.                                  (my-res (car res) ;;tworzy pare z wartości
  211.                                        (set-mem 'seed (cdr res) m))));;i pamięci z zupdatowanym seedem
  212.                     (second e))] ;;nakarmi randa drugim elementem
  213.         [(op? e)
  214.          (let ((res (mymap (op-args e) m))) ;;wykona
  215.          (my-res
  216.           (apply (op->proc (op-op e)) (car res)) ;; (car res) trzyma zmapowana liste eval-arithem
  217.                  (cdr res)))] ;;memory
  218.         [(const? e) (my-res e m)]))
  219.  
  220.  
  221. (define (mymap args m) ;;dziala jak map z napisany iteracyjne zwraca pare (zmapowana liste i pamiec)
  222.   (define (iter arg acc m) ;;mapuje eval-arithem z ciągle przekazywaną pamięcią
  223.     (if (null? arg)
  224.         (cons (reverse acc) m)
  225.         (let ((res (eval-arith-n (car arg) m)))
  226.           (iter (cdr arg) (cons (my-val res) acc) (my-mem res)))))
  227.   (iter args null m))
  228.  
  229.  
  230.  
  231.  
  232. (define (run e)
  233.   (eval e empty-mem initial-seed))
  234.  
  235. ;;ZAD A
  236.  
  237. (define fermat-test
  238.   '{
  239.     (composite := false)
  240.     (while (and (> k 0) (not composite) (not (= n 2)) (not (= n 3))) { ;;2 i 3 wyrzucam
  241.             (a := (+ 2 (rand (- n 3))))
  242.             (if (not (= (mod (expt a (- n 1)) n) 1))
  243.                 (composite := true) ())              
  244.             (k := (- k 1))})                
  245.     }
  246.   )
  247.  
  248. (define (probably-prime? n k) ; check if a number n is prime using
  249.                               ; k iterations of Fermat's primality
  250.                               ; test
  251.   (let ([memory (set-mem 'k k
  252.                 (set-mem 'n n empty-mem))])
  253.     (not (get-mem
  254.            'composite
  255.            (eval fermat-test memory initial-seed)))))
  256.  
  257. (define (test n k) ;;do debugowania pokaże pamięć a nie tylko #t #f
  258.   (let ([memory (set-mem 'k k
  259.                 (set-mem 'n n empty-mem))])
  260.            (eval fermat-test memory initial-seed)))
  261.  
  262.  
  263. (display "101? ")
  264. (probably-prime? 101 1000)
  265. (display "2? ")
  266. (probably-prime? 2 100)
  267. (display "3? ")
  268. (probably-prime? 3 100)
  269. (display "4? ")
  270. (probably-prime? 4 10)
  271. (display "5? ")
  272. (probably-prime? 5 100)
  273. (display "10? ")
  274. (probably-prime? 10 100)
  275. (display "14? ")
  276. (probably-prime? 14 88)
  277. (display "9? ")
  278. (probably-prime? 9 11)
  279. (display "21? ")
  280. (probably-prime? 21 37)
  281. (display "13? ")
  282. (probably-prime? 13 37)
  283. (display "42? ")
  284. (probably-prime? 42 100)
  285. (display "18? ")
  286. (probably-prime? 18 100)
  287. (display "100? ")
  288. (probably-prime? 100 100)
  289. (display "20443? ")
  290. (probably-prime? 20443 100)
  291. (display "22501? ")
  292. (probably-prime? 22501 10)
  293. (display "95111? ")
  294. (probably-prime? 95111 10)
  295. (display "1000000? ")
  296. (probably-prime? 1000000 10)
  297. (display "104639? ")
  298. (probably-prime? 104639 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement