[plt-scheme] creating an instance of a structure

From: Alex Peake (apeake at comac.com)
Date: Fri Jul 12 11:00:21 EDT 2002

This is how I did it in v103:


(defstruct attr name domain dom-len dom-prec nullable pk-pos fk-pos fk-table fk-attr identity)


(define attr
  (lambda (name domain . args)
    ;;default values
    (let ((dom-len (getarg args :dom-len #f))
          (dom-prec (getarg args :dom-prec #f))
          (nullable (getarg args :nullable #f))
          (pk-pos (getarg args :pk-pos #f))
          (fk-pos (getarg args :fk-pos #f))
          (fk-table (getarg args :fk-table #f))
          (fk-attr (getarg args :fk-attr name))
          (identity (getarg args :identity #f)))
      (make-attr name domain dom-len dom-prec nullable pk-pos fk-pos fk-table fk-attr identity))))

;; Scans a Lisp-style keyword/value property list.
(define (getarg initargs name . not-found)
  (define (scan tail)
    (cond ((null? tail) (if (pair? not-found)
                            (car not-found)
                            (error 'getarg "couldn't find ~e." name)))
          ((null? (cdr tail)) (error 'getarg "keyword list not balanced."))
          ((eq? (car tail) name) (cadr tail))
          (else (scan (cddr tail)))))
  (scan initargs))

;; The same as getarg but return the list of all key values matched - no need
;; for a default value.  The result is in the same order.
(define (getargs initargs name)
  (define (scan tail)
    (cond ((null? tail) '())
          ((null? (cdr tail)) (error 'getargs "keyword list not balanced."))
          ((eq? (car tail) name) (cons (cadr tail) (scan (cddr tail))))
          (else (scan (cddr tail)))))
  (scan initargs))



-----Original Message-----
From: plt-scheme-admin at list.cs.brown.edu
[mailto:plt-scheme-admin at list.cs.brown.edu]On Behalf Of Rohan Nicholls
Sent: Friday, July 12, 2002 3:28 AM
To: Plt-Scheme (E-mail)
Subject: [plt-scheme] creating an instance of a structure


[Please handle PLT Scheme list administrative tasks through the Web:
   http://list.cs.brown.edu/mailman/listinfo/plt-scheme]

I have looked through the emails I have for structure information, and have now updated to v200 so
the fixes are in.

In the manual on mzscheme the example on creating and using structs is a little confusing and I am
hoping someone can help.  Please forgive me as I am coming from a Lisp perspective where named
arguments are used to define and access elements.

I have been going through Learn Scheme in Fixnum Days, which I am enjoying however the chapter on
structures seems to be a little off the mark.

First of all it is define-struct not defstruct, figured that one myself, and making a simple tree
structure is easy, however here is where things get sticky:

(define-struct tree (height girth age leaf-shape leaf-colour)) ; to define the structure

t-y-s's version of creating an instance:
(define elm (make-tree
	     'height 30
	     'leaf-shape 'frond
	     'leaf-colour 'green))
This gives:
make-tree: expects 5 arguments, given 6: height 30 leaf-shape frond leaf-colour green

The example in the MzScheme manual is very trivial for brevity, so the initialization does not need
to name the elements, and initializes them all. Is there an equivalent to the above that works in
Dr.Scheme/mzscheme?  At the moment it will only accept a straight up 5 arguments and I don't seem to
be able to skip any, or give entries for specific slots.

This leads to the next problem, accessing and setting the slots later:
t-y-s says:
The accessor procedures are invoked as follows:

(tree.height coconut) =>  30
(tree.leaf-shape coconut) =>  frond
(tree.girth coconut) =>  <undefined>

The modifier procedures are invoked as follows:

(set!tree.height coconut 40)
(set!tree.girth coconut 10)

The above is what I would expect, coming from Lisp, but this doesn't work and mzscheme again is a
little quiet on the subject.  I feel like I am missing something incredibly obvious, can someone set
me on the path to enlightenment?

Thanks,

Rohan






Posted on the users mailing list.