Advertisement
Guest User

Tuplanolla

a guest
May 30th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.07 KB | None | 0 0
  1. #lang r5rs
  2.  
  3. (#%require srfi/39)
  4. (#%require (prefix base: racket/base)
  5.            (prefix base: racket/function))
  6. (#%require (prefix match: racket/match))
  7.  
  8. (define (make-method)
  9.   (make-parameter 'unimplemented-method))
  10.  
  11. (define-syntax forall
  12.   (syntax-rules ()
  13.     ((_ arg ...) (lambda arg ...))))
  14.  
  15. (base:define-namespace-anchor anchor)
  16.  
  17. (define (with-instance-thunk inst thunk)
  18.   (eval `(parameterize ,inst (,thunk))
  19.         (base:namespace-anchor->namespace anchor)))
  20.  
  21. (define-syntax with-instance
  22.   (syntax-rules ()
  23.     ((_ inst body ...)
  24.      (with-instance-thunk inst (lambda () body ...)))))
  25.  
  26. (define fmap-method (make-method))
  27. (define (fmap f a)
  28.   ((fmap-method) f a))
  29.  
  30. (define functor-identity?
  31.   (forall (a)
  32.     (equal? (fmap base:identity a)
  33.             a)))
  34.  
  35. (define functor-compose?
  36.   (forall (f g a)
  37.     (equal? (fmap (base:compose f g) a)
  38.             (fmap f (fmap g a)))))
  39.  
  40. (define list-functor
  41.   `((fmap-method ,(lambda (f a) (map f a)))))
  42.  
  43. (with-instance list-functor
  44.   (display (fmap (lambda (x) (+ 1 x)) (list 42 13)))
  45.   (newline))
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement