Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created April 6, 2012 13:30
Show Gist options
  • Save nyuichi/2319798 to your computer and use it in GitHub Desktop.
Save nyuichi/2319798 to your computer and use it in GitHub Desktop.
de bruijn index -> number
(define n-0 '(L (L (V 0))))
(define n-1 '(L (L (A (V 1) (V 0)))))
(define n-2 '(L (L (A (V 1) (A (V 1) (V 0))))))
(define n-3 '(L (L (A (V 1) (A (V 1) (A (V 1) (V 0)))))))
(define (unwrap n) (cadr (cadr n)))
(define (V? x) (eq? (car x) 'V))
(define (A? x) (eq? (car x) 'A))
(define (A-expr2 x) (caddr x))
(define (conv- m)
(cond
((V? m) 0)
((A? m) (+ (conv- (A-expr2 m)) 1))))
(define (conv n) (conv- (unwrap n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment