On this page:
gjs/  cselim
occurs-in?

2.4 gjs-cselim🔗

 (require sicm/general/gjs-cselim) package: rktsicm

Some utilities for finding common subexpressions

procedure

(gjs/cselim expression    
  [not-worth-subdividing?])  any/c
  expression : any/c
  not-worth-subdividing? : predicate/c = (λ (x) #f)
Given a (symbolic) expression, find the common subexpressions and rewrite the expression inserting let’s at the highest possible level. Any expression that is not-worth-subdividing? will be left as is. gjs/cselim recognizes lambda, λ and plain let as introducing new scope. Named let, let-values, definitions, or other mechanisms that introduce new bindings will probably give the wrong result.

Examples:
> (gjs/cselim '(lambda (x)
                 (/ (+ (* x 3) (- y z) (- x y) (* x 3))
                    (- y z))))

'(let ((g184194 (- y z))) (lambda (x) (let ((g184193 (* x 3))) (/ (+ g184193 g184194 (- x y) g184193) g184194))))

> (gjs/cselim '(list (+ 4 x)
                     (let* ([x 4]
                            [y (+ 4 x)])
                       (+ (* x y) (/ (* x y) (+ 4 x))))))

'(let ((g184199 (+ 4 x)) (g184203 (* x y))) (list g184199 (let* ((x 4) (y g184199)) (+ g184203 (/ g184203 g184199)))))

> (gjs/cselim '(list (+ 4 x)
                     (let ([x 4]
                           [y (+ 4 x)])
                       (+ (* x y) (/ (* x y) (+ 4 x))))))

'(let ((g184208 (+ 4 x))) (list g184208 (let ((x 4) (y (+ 4 x))) (let ((g184209 (* x y))) (+ g184209 (/ g184209 (+ 4 x)))))))

procedure

(occurs-in? variables expression)  (or/c #f #t list?)

  variables : any/c
  expression : any/c
Check if any of the variables occur in the expression, but also works if variables or expression is an object instead of a list.