are there any amcros that play well with destructuring bind of define-type's ? I just want to check to see if there's a pre-existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
Thanks!
Afficher les réponses par date
2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
are there any amcros that play well with destructuring bind of define-type's ? I just want to check to see if there's a pre-existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
This is the kind of thing where macros get over-used
(define (with-foo f body) (body (foo-a f) (foo-b f)))
david
macro almost there :-)
given (define-type point x y z)
if at run time, I have the symbol "point", is there anyway that I can make that will return me the name of the fields, i.e. '(x y z)? I'd rather have this than all functions that start with point-* since I also define some of them myself, like point-+ point-scale, ...
Thanks!
On Mon, Jun 1, 2009 at 9:45 PM, David Rush kumoyuki@gmail.com wrote:
2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
are there any amcros that play well with destructuring bind of
define-type's
? I just want to check to see if there's a pre-existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
This is the kind of thing where macros get over-used
(define (with-foo f body) (body (foo-a f) (foo-b f)))
david
GPG Public key at http://cyber-rush.org/drr/gpg-public-key.txt
Suppose I have the following:
(define-type p3 x y z)
(define (with-cons c func) (func (car c) (cdr c))))
(define (with-p3 p func) (func (p3-x p) (p3-y p) (p3-z p)))
(define p (cons (make-p3 1 2 3) (make-p3 4 5 6)))
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
How can I do this with with-cons / with-p3? (It seems I can only go done one level of nesting).
Thanks!
On Mon, Jun 1, 2009 at 9:45 PM, David Rush kumoyuki@gmail.com wrote:
2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
are there any amcros that play well with destructuring bind of
define-type's
? I just want to check to see if there's a pre-existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
This is the kind of thing where macros get over-used
(define (with-foo f body) (body (foo-a f) (foo-b f)))
david
GPG Public key at http://cyber-rush.org/drr/gpg-public-key.txt
lowly coder wrote:
Suppose I have the following:
(define-type p3 x y z)
(define (with-cons c func) (func (car c) (cdr c))))
(define (with-p3 p func) (func (p3-x p) (p3-y p) (p3-z p)))
(define p (cons (make-p3 1 2 3) (make-p3 4 5 6)))
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
How can I do this with with-cons / with-p3? (It seems I can only go done one level of nesting).
I think you might want to investigate time into using an object system with generic functions... Gambit's define-type is juste a raw data container with not much meat around it...
That said, if the usage your mentionning is just want you want to do, then you can hardcode the macro for pairs of p3 objects:
(define-macro (destructure-bind pair-p3 . body) (let ((arg (gensym 'arg)) (p1 (gensym 'p1)) (p2 (gensym 'p2))) `(let* ((,arg ,pair-p3) (,p1 (car ,arg)) (,p2 (cdr ,arg)) (x1 (p3-x ,p1)) (y1 (p3-y ,p1)) (z1 (p3-z ,p1)) (x2 (p3-x ,p2)) (y2 (p3-y ,p2)) (z2 (p3-z ,p2))) ,@body)))
(destructure-bind (cons (make-p3 1 2 3) (make-p3 4 5 6))
(make-p3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
#<p3 #6 x: 5 y: 7 z: 9>
I hope it can help you...
David
Thanks David,
I actually ended up ending up a generic macro destructurer like the one you have above (except it's not hard coded to anything, it looks at the first elem of the list (whether it's a cons, a p3, a ...) then figures out how to destructure it based on that. Fairly ugly macro, but elegant to use.
On Tue, Jun 2, 2009 at 5:36 AM, David St-Hilaire sthilaid@iro.umontreal.cawrote:
lowly coder wrote:
Suppose I have the following:
(define-type p3 x y z)
(define (with-cons c func) (func (car c) (cdr c))))
(define (with-p3 p func) (func (p3-x p) (p3-y p) (p3-z p)))
(define p (cons (make-p3 1 2 3) (make-p3 4 5 6)))
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
How can I do this with with-cons / with-p3? (It seems I can only go done one level of nesting).
I think you might want to investigate time into using an object system with generic functions... Gambit's define-type is juste a raw data container with not much meat around it...
That said, if the usage your mentionning is just want you want to do, then you can hardcode the macro for pairs of p3 objects:
(define-macro (destructure-bind pair-p3 . body) (let ((arg (gensym 'arg)) (p1 (gensym 'p1)) (p2 (gensym 'p2))) `(let* ((,arg ,pair-p3) (,p1 (car ,arg)) (,p2 (cdr ,arg)) (x1 (p3-x ,p1)) (y1 (p3-y ,p1)) (z1 (p3-z ,p1)) (x2 (p3-x ,p2)) (y2 (p3-y ,p2)) (z2 (p3-z ,p2))) ,@body)))
(destructure-bind (cons (make-p3 1 2 3) (make-p3 4 5 6))
(make-p3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
#<p3 #6 x: 5 y: 7 z: 9>
I hope it can help you...
David
2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
Suppose I have the following:
(define-type p3 x y z) (define (with-cons c func) (func (car c) (cdr c)))) (define (with-p3 p func) (func (p3-x p) (p3-y p) (p3-z p))) (define p (cons (make-p3 1 2 3) (make-p3 4 5 6)))
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
How can I do this with with-cons / with-p3? (It seems I can only go done one level of nesting).
*sigh* The naming will be confusing because of your previous choices, but...
(with-cons p (lambda (p1 p2) (with-p3 p1 (lambda (x1 y1 z2) (with-p3 p2 (lambda (x2 y2 z2) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)) ))))))
If you are implementing a bunch of dyadic operations you can of course encapsulate this via recursive application of the pattern. This is better than a macro because it is, in fact, more flexible; to say nothing of the phasing issues that can come into play when you start building up a large macro library.
This is an example of a general Scheme style rule which doesn't seem to get propagated so much anymore in c.l.s: when given a choice between using a function and a macro, using a function is almost always the right choice. It is certainly true until you learn just how powerful LAMBDA and full TCO really are.
david rush
2009/6/2 David Rush kumoyuki@gmail.com:
2009/6/2 lowly coder lowlycoder@huoyanjinjing.com:
Now, ideally I want to write:
(destructure-bind (cons (p3 x1 y1 z1) (p3 x2 y2 z2)) (make-point3 (+ x1 x2) (+ y1 y2) (+ z1 z2)))
I should add that if you really want generic pattern matching, that such packages do exist for Scheme, although I am not sure that there are any written in R[45]RS compatible dialects - they may all be implementation specific. Troll c.l.s on google or just ask about structural pattern matching...
david
On 2-Jun-09, at 12:24 AM, lowly coder wrote:
are there any amcros that play well with destructuring bind of define-type's ? I just want to check to see if there's a pre- existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
Try this (but beware that the API for the ## procedures may change in the future):
(define-macro (def-type . args) (##define-type-parser 'def-type #f args (lambda (name flags id extender constructor constant-constructor predicate implementer type-exhibitor prefix fields total-fields) (let ((field-names (map car fields))) `(begin (define-type ,@args) (define (,(##symbol-append 'with- name) obj proc) (proc ,@(map (lambda (f) `(,(##symbol-append name '- f) obj)) field-names))))))))
(def-type point x y)
(define p (make-point 11 22))
(with-point p (lambda (x y) (pp (list 'x+y= (+ x y)))))
Marc
Thanks Marc,
I was pondering on this earlier today, and it struck me a really simple solution is this:
types.scm <-- not really scheme, but defines the types I want to use
destructure.scm <-- reads data in from types.scm
prog.scm <-- reads data in from types.scm, and executes the right define-types
:-)
On an unrelated note, what is this "heartbeat-interval", and how can I read it? (I can only set it via ##thread-heartbeat-interval-set!) ... it seems very key in the functionality of statprof.scm (it appears that statprof.scm depends on an interrupt being fired so often, then looking at the current state of the interpreter, and thus buiding a statistical view of the program).
Knowing the value of heartbeat-interval is _very_ important to me. In particular, I have a graphics server running at 100hertz. Suppose heartbeat-interval is also 100hz, I can easily get highly distorted values from statprof.scm
Thanks!
On Tue, Jun 2, 2009 at 7:56 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 2-Jun-09, at 12:24 AM, lowly coder wrote:
are there any amcros that play well with destructuring bind of
define-type's ? I just want to check to see if there's a pre-existing solution before re-inventing my own.
I find myself writing way too much code of the form:
(define-type foo a b) (define-type bar c d)
(lambda (x) (let ((c (bar-c x) (d (bar-d x))) (let ((a (foo-a a) (b (foo-b b)) ...
Try this (but beware that the API for the ## procedures may change in the future):
(define-macro (def-type . args) (##define-type-parser 'def-type #f args (lambda (name flags id extender constructor constant-constructor predicate implementer type-exhibitor prefix fields total-fields) (let ((field-names (map car fields))) `(begin (define-type ,@args) (define (,(##symbol-append 'with- name) obj proc) (proc ,@(map (lambda (f) `(,(##symbol-append name '- f) obj)) field-names))))))))
(def-type point x y)
(define p (make-point 11 22))
(with-point p (lambda (x y) (pp (list 'x+y= (+ x y)))))
Marc