[racket] Are there Racket structures similar to Common Lisp's?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Aug 17 08:58:47 EDT 2012

p.s. Here is a naive macro that gets you close, assuming you're willing to expand to classes: 

#lang racket

;; -----------------------------------------------------------------------------
;; library code:
(define-syntax-rule 
  (defstruct s f ...)
  (begin (define x (class object% (init-field (f '()) ...) (super-new)))
         (define-syntax-rule
           (s a (... ...))
           (new x a (... ...)))))

;; -----------------------------------------------------------------------------
;; client code 
(defstruct person name age waist-size favorite-color)

(define p (person (age 35) (favorite-color "blue") (name "Bob")))

(get-field age p)
(get-field waist-size p)




On Aug 17, 2012, at 8:35 AM, Matthias Felleisen wrote:

> 
> It does not exit at the moment, but it should be macro definable. 
> If you do develop a defstruct library, let me suggest you use 
> Racket keywords for field tags: 
> 
>  (person #:age 35 #:favorite-color "blue" #:name "Bob")
> 
> As for NIL as default value, I question this idea very much. 
> But then, Racket isn't Common Lisp and Common Lisp isn't Racket. 
> 
> 
> 
> 
> 
> On Aug 17, 2012, at 4:20 AM, Rouben Rostamian wrote:
> 
>> No, not really.  I am looking for structures a la Common Lisp,
>> as described in the example that I gave.  The essential feature
>> there is that the constructor may receive the structure's fields
>> in any order.  There is no ambiguity because each field value
>> is associated with field tag.
>> 
>> I think I should be able to come up with a reverse-engineered
>> version of that in Racket but I don't want to reinvent the wheel
>> if it already exists.
>> 
>> -- rr
>> 
>> On Thu, Aug 16, 2012 at 10:44:08PM -0600, Richard Cleis wrote:
>>> 
>>> Are you asking for stuff like this?
>>> 
>>> http://docs.racket-lang.org/reference/define-struct.html
>>> 
>>> On Aug 16, 2012, at 10:09 PM, Rouben Rostamian wrote:
>>> 
>>>> Is there the equivalent of Common Lisp's /structure/ type in
>>>> Racket?  I looked through Racket's User Guide and Reference
>>>> manuals but did not see something similar, although it's
>>>> quite possible that I saw one but did not recognize it.
>>>> 
>>>> Specifically, here is what I want.  Consider the Common Lips
>>>> definition of a /person/ structure:
>>>> 
>>>> (defstruct person
>>>>         name
>>>>         age
>>>>         waist-size
>>>>         favorite-color)
>>>> 
>>>> Then create an instance:
>>>> 
>>>> (make-person :age 35
>>>>           :favorite-color "blue"
>>>>           :name "Bob")
>>>> 
>>>> The latter echos:
>>>> 
>>>> #S(PERSON :NAME "Bob" :AGE 35 :WAIST-SIZE NIL :FAVORITE-COLOR "blue")
>>>> 
>>>> Let's note that the order of fields when invoking /make-person/
>>>> need not be the same as the order in which the /person/
>>>> structure is defined.
>>>> 
>>>> Furthermore, since /waist-size/ was not specified at invocation,
>>>> it was assigned NIL.
>>>> 
>>>> -- 
>>>> Rouben Rostamian
>>>> 
>>>> ____________________
>>>> Racket Users list:
>>>> http://lists.racket-lang.org/users
>>> 
>>> 
>> ____________________
>> Racket Users list:
>> http://lists.racket-lang.org/users
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4373 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20120817/528b84d1/attachment.p7s>

Posted on the users mailing list.