<br><br><div class="gmail_quote">On Sun, May 22, 2011 at 6:39 AM, Mikael <span dir="ltr"><<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="gmail_quote"><div><br>Why would you want this? </div></div></blockquote><div><br> As Adrien mentioned, it's a common practice in other languages, which I 
got used to (Python: override __repr__(), c++: override 
operator<<(), Java: override ToString(), etc). In my particular 
case, I was writing a doubly-linked list type, and wanted it to print 
(up to) its first three and last three values when displayed in the 
REPL.<br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote"><div>Anyhow, you could make your own Gambit patch that adds a pp: procedure argument to define-type. Feel free to publish your patch on the Dumping grounds.<br>

</div></div><br></blockquote><div><br>I'll start by learning how to write macros! :D<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="gmail_quote"><div> <br></div><div class="im"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">



Similarly, how can I define a custom constructor that does something more complicated than assign arguments to slots? <br></blockquote></div><div><br>By defining your own procedure that does the job.<br></div></div></blockquote>

<div><br>I figured it out. Here's an expanded answer for other beginners. It's actually as easy as:<br><br>(define-type my-type<br>  ...<br>  constructor: (lambda (<args>) <body>)<br>  ...)<br><br></div>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote"><br><div>Someone else please verify that Gambit's define-record-type is just an alternative name for define-type, just like what call/cc is to call-with-current-continuation .<br>

</div></div></blockquote><div><br>It seems that they aren't equivalent. Gambit's define-record-type comes from srfi-9, which uses positional arguments rather than define-type's keyword arguments:<br><br><a href="http://srfi.schemers.org/srfi-9/srfi-9.html">http://srfi.schemers.org/srfi-9/srfi-9.html</a><br>


<br><font size="2">From the ex</font>ample in the above link, we see that you can define a "pare" (pair) type as <span style="font-family: arial,helvetica,sans-serif;">follows</span>:<br><pre>(define-record-type :pare<br>

    (kons x y)<br>    pare?<br>    (x kar set-kar!)<br>    (y kdr))<br><br>We can then do a quick smoke-check to see that it worked:<br></pre>
<pre>> (pare? (kons 1 2))<br>#t<br><br>If we restart the interpreter and repeat the above two steps, this time using define-type instead of define-record type, we get:<br><br>> (define-type :pare (kons x y) pare? (x kar set-kar!) (y kdr))<br>

> (pare? (kons 1 2))<br>*** ERROR IN (console)@2.2 -- Unbound variable: pare?<br><br>That said, define-record-type does seem to be defined in terms of define-type, as can be seen in ~~/lib/nonstd.scm. Also, define-structure is equivalent to define-type:<br>

<br>(define-runtime-macro (define-type . args)<br>  (##define-type-expand 'define-type #f #f args))<br><br>(define-runtime-macro (define-structure . args)<br>  (##define-type-expand 'define-structure #f #f args))<br>

<br>(define-runtime-macro (define-record-type name constructor predicate . fields)<br>  `(define-type ,name<br>     constructor: ,constructor<br>     predicate: ,predicate<br>     ,@fields))<br><br>-- Matt<br><br></pre></div>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<br>
<br>_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca" target="_blank">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br></blockquote></div><br>
<br>_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br></blockquote></div><br>