[gambit-list] New types
Marc Feeley
feeley at IRO.UMontreal.CA
Tue Feb 8 16:11:31 EST 2005
> Is there a way to create new primitive types in Gambit? I'm using 3.0
> but I'm interested in answers regarding either version.
In 3.0 and 4.0 you can use define-structure:
Gambit Version 3.0
> (define-structure foo a b)
> (define x (make-foo 11 22))
> x
#s(foo (a 11) (b 22))
> (foo? x)
#t
> (foo? car)
#f
> (vector? x)
#f
If you really want to keep your procedural representation, you can
access a procedure's "code" pointer to distinguish closures created
from one lambda from closures created from another lambda. Here's some
sample code:
(define eq-procedure-code? ; this handles compiled and interpreted code
(lambda (proc1 proc2)
(if (##closure? proc1)
(and (##closure? proc2)
(if (##interp-procedure? proc1)
(and (##interp-procedure? proc2)
(eq? (##interp-procedure-code proc1)
(##interp-procedure-code proc2)))
(eq? (##closure-code proc1)
(##closure-code proc2))))
(eq? proc1 proc2))))
(define make-adder
(lambda (x)
(lambda (y)
(+ x y))))
(define make-multiplier
(lambda (x)
(lambda (y)
(* x y))))
(define f (make-adder 1))
(define g (make-adder 2))
(define h (make-multiplier 3))
(pp (eq-procedure-code? f car)) ; => #f
(pp (eq-procedure-code? f g)) ; => #t
(pp (eq-procedure-code? g h)) ; => #f
Marc Feeley
More information about the Gambit-list
mailing list