[racket] Are there Racket structures similar to Common Lisp's?
Thank you all for the numerous comments and suggestions
regarding Lisp-style structures for Racket. Following
Danny Yoo's hint, I put together a perhaps kludgy
way of achieving the effect I was looking for.
Here it is:
;--- begin demo ----------------------------------------------------
; A /person/ is a structure (The '#:transparent' flag is optional):
(struct person (name age waist-size favorite-color) #:transparent)
; A /person/ constructor. The argument /name/ is required; the
; other arguments are optional and are given default values of #f:
(define (make-person
name
#:age [age #f]
#:waist-size [waist-size #f]
#:favorite-color [favorite-color #f])
(person name age waist-size favorite-color))
; Let's make a person:
(make-person "John")
=> (person "John" #f #f #f)
; Another one:
(make-person #:age 35 #:waist-size 24 "Betty")
=> (person "Betty" 35 24 #f)
; Another one:
(define p (make-person #:age 35 "Alice" #:favorite-color "red"))
(person-name p)
=> "Alice"
(person-age p)
=> 35
(person-waist-size p)
=> #f
;--- end demo ----------------------------------------------------
Perhaps this can be jazzed up through the use of macros but
I haven't tried.
Rouben
---- original message -------------------------------------------
From: Rouben Rostamian <rostamian at umbc.edu>
Date: Fri, 17 Aug 2012 00:09:34 -0400
To: users at racket-lang.org
Subject: [racket] Are there Racket structures similar to Common Lisp's?
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