0
votes

i'm having trouble defining some code in scheme. I am trying to create a record for a node in Scheme/Racket, so far my code looks as follows:

(define-record-type node

(make-node v l r)
node?
(v tree-value)
(l tree-left)
(r tree-right))

However - when I try and execute I get the following error:

define-record-type: expected amutable', immutable',parent', protocol',sealed', opaque',nongenerative', or parent-rtd' clause in: (make-node v l r)

I understand that you can define field types to be mutable, immutable etc. But I can't see why this needs to be a clause in the constructor - and if so where it should be?

Could someone please explain

1)How to get around this error

2)Why it arises

1
This error occurs with #!r6rs, but not with #lang racket followed by (require srfi/9).uselpa
Thanks, can you explain why? Or how I would get around this without requiring srfi/9? Also if it's #lang scheme can it be solved with (require srfi/9) ?Adam Barrett
In Racket, the idiomatic thing to do is to define records using struct with #lang racket. Note that #lang scheme is a legacy setting that isn't meant for use in new code.Asumu Takikawa

1 Answers

0
votes

Your code is not in accordance with the R6RS standard reference doc, possible definitions are:

#!r6rs
(import (rnrs) (rnrs records syntactic))

(define-record-type node (fields v l r))
(define tree-value (record-accessor node 0))
(define tree-left  (record-accessor node 1))
(define tree-right (record-accessor node 2))

or

#!r6rs
(import (rnrs) (rnrs records syntactic))

(define-record-type node 
  (fields 
   (immutable v tree-value)
   (immutable l tree-left)
   (immutable r tree-right)))

or

#!r6rs
(import (rnrs) (rnrs records syntactic))

(define-record-type (node make-node node?)
  (fields 
   (immutable v tree-value)
   (immutable l tree-left)
   (immutable r tree-right)))

For any of those, you get:

(define n (make-node 1 2 3))

(display n) (newline)
=> #(struct:node 1 2 3)

(display (node? n)) (newline)
=> #t

(display (tree-value n)) (newline)
=> 1

(display (tree-left n)) (newline)
=> 2

(display (tree-right n)) (newline)
=> 3