PasteRack.org
Paste # 53563
2018-06-15 19:33:26

Fork as a new paste.

Paste viewed 127 times.


Embed:

  1. #lang racket
  2.  
  3. (define (deepmap proc lst #:limit [limit (lambda (_) #t)])
  4.   (map (lambda (x)
  5.          (if (limit x)
  6.            (if (list? x)
  7.              (deepmap proc x #:limit limit)
  8.              (proc x))
  9.            x))
  10.        lst))
  11.  
  12. (define (replacer from to)
  13.   (lambda (x)
  14.     (if (equal? x from)
  15.       to
  16.       x)))
  17.  
  18. (define (limit sym x)
  19.   (displayln x)
  20.   (match x
  21.     [`(function ,x ,b ...)
  22.       (if (member sym x)
  23.         #f
  24.         #t
  25.         )]
  26.     [_ #t]))
  27.  
  28. (deepmap
  29.   (replacer 'a 'c) ; Reducing a to c
  30.   ;; The code
  31.   `(control a (b whatever c (function (a) a a a) (function (b) a b c) plus a) no a c)
  32.   ;; Preventing deepmap from entering functions which have a as a parameter (preserving alpha equiv)
  33.   #:limit (curry limit 'a))

=>

control

a

(b whatever c (function (a) a a a) (function (b) a b c) plus a)

b

whatever

c

(function (a) a a a)

(function (b) a b c)

function

(b)

b

a

b

c

plus

a

no

a

c

'(control

  c

  (b whatever c (function (a) a a a) (function (b) c b c) plus c)

  no

  c

  c)