Thanks for the suggestion Marc. I think this will work perfectly.
Here is the reason that I'd like to stick with the procedural representation instead of using Gambit structures directly.
(define-structure point x y)
(define-structure airplane pos vel)
(define a (make-airplane (make-point 10 10) (make-point 0 0)))
;; Accessing the x coordinate of the airplane:
(point-x (airplane-pos a))
;; In an object system like the one I made it's much shorter:
[[a pos:] x:]
Getting at an object inside an object inside an object... in a struct/record system that typically accompanies Scheme implementations can be a pain in that it's so verbose. Even in C all you have to write is the name of the variable and the name of the field (plus the dot or arrow).
"Different things should look different". By convention I use brackets whenever accessing or setting a field in an object. To set the value of x in the airplane above it would look like this:
[[a pos:] x: 5]
As opposed to this:
(point-x-set! (airplane-pos a) 5)
Structs in Xlib have pretty verbose names. It's nice to say this:
[attr border_pixmap:]
Instead of this:
(XSetWindowAttributes-border_pixmap attr)
I can think of more examples, but anyone that has programmed large Scheme projects surely knows this pain.
In Gambit we have two styles of keywords. foo: and :foo. I use the post-colon keywords to denote fields. As another convention, I considered using pre-colon keywords to denote methods.
Eduardo Cavazos
On Tue, 8 Feb 2005 16:11:31 -0500, Marc Feeley feeley@iro.umontreal.ca wrote:
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
Afficher les réponses par date
On Thu, 10 Feb 2005 20:58:02 -0600, Eduardo Cavazos wayo.cavazos@gmail.com wrote:
Here is the reason that I'd like to stick with the procedural representation instead of using Gambit structures directly.
...
;; In an object system like the one I made it's much shorter:
Well, I used to think that way too. Believe me, you're on well-trodden ground here.
Anyway I switched to heavy use of functors because I got tired of having lots of
(let ((accessor (object accessor-keyword))) ..)
code and because functors have a much more flexible pattern of reuse. This has had the added advantage of allowing me to re-use native implementations of structures as in Gambit. In some Schemes (most notably Stalin, but this also applies to PLT), using native structures can result in significant speed-ups to your code. I am not sure if that is true in Gambit-4 or not.
david rush
What's a functor? Can you show me an example of how they are better in some way?
On Mon, 14 Feb 2005 09:24:59 +0530, david rush kumoyuki@gmail.com wrote:
On Thu, 10 Feb 2005 20:58:02 -0600, Eduardo Cavazos wayo.cavazos@gmail.com wrote:
Here is the reason that I'd like to stick with the procedural representation instead of using Gambit structures directly.
...
;; In an object system like the one I made it's much shorter:
Well, I used to think that way too. Believe me, you're on well-trodden ground here.
Anyway I switched to heavy use of functors because I got tired of having lots of
(let ((accessor (object accessor-keyword))) ..)
code and because functors have a much more flexible pattern of reuse. This has had the added advantage of allowing me to re-use native implementations of structures as in Gambit. In some Schemes (most notably Stalin, but this also applies to PLT), using native structures can result in significant speed-ups to your code. I am not sure if that is true in Gambit-4 or not.
david rush
DIsruptive Technology! _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca http://mailman.iro.umontreal.ca/mailman/listinfo/gambit-list