Advertisement
Void-voiD

Untitled

Dec 8th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. (define-syntax trace
  2. (syntax-rules ()
  3. ((trace x)
  4. (begin
  5. (write (quote x))
  6. (display " => ")
  7. (write x)
  8. (newline)
  9. x))))
  10.  
  11. (define (super-car x wc cur)
  12. (if (= wc cur)
  13. (car x)
  14. (super-car (cdr x) wc (+ cur 1))))
  15.  
  16. (define (super-cons x res)
  17. (if (null? x)
  18. (reverse res)
  19. (super-cons (cdr x) (cons (car x) res))))
  20.  
  21. (define-syntax define-data
  22. (syntax-rules ()
  23. ((_ type (arg . args))
  24. (begin
  25. (for-each (lambda (i)
  26. (begin
  27. (eval `(define (,(car i) x . xx)
  28. (list->vector (cons 'type (cons ',(car i) (cons x (super-cons xx '())))))) (interaction-environment)))) (cons 'arg 'args))
  29. (eval `(define (,(string->symbol (string-append (symbol->string 'type) (symbol->string '?))) x)
  30. (and (vector? x) (equal? 'type (vector-ref x 0)) (> (vector-length x) 2))) (interaction-environment))
  31. ))))
  32.  
  33. (define-syntax match
  34. (syntax-rules ()
  35. ((_ true ((name arg ...) expr) ...)
  36. (cond
  37. ((equal? (vector-ref true 1) (name ...)) (apply (lambda (arg ... ...) (expr ...)) (vector->list (cddr true))))))))
  38.  
  39. (begin
  40. (define-data figure ((square a)
  41. (rectangle a b)
  42. (triangle a b c)
  43. (circle r)))
  44.  
  45. (define s (square 10))
  46. (define r (rectangle 10 20))
  47. (define t (triangle 10 20 30))
  48. (define c (circle 10))
  49.  
  50. (define test-1 (and (figure? s)
  51. (figure? r)
  52. (figure? t)
  53. (figure? c)))
  54.  
  55. (define a '(circle 0 0 1))
  56. (define b #f)
  57.  
  58. (define test-2 (and (figure? a)
  59. (figure? b)))
  60.  
  61. (define pi (acos -1))
  62.  
  63. (define (perim f)
  64. (match f
  65. ((square a) (* 4 a))
  66. ((rectangle a b) (* 2 (+ a b)))
  67. ((triangle a b c) (+ a b c))
  68. ((circle r) (* 2 pi r))))
  69.  
  70. (list test-1
  71. test-2
  72. (perim s)
  73. (perim r)
  74. (perim t)
  75. (round (perim c)))
  76. ) ; end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement