zendevil.eth
zendevil.eth

Reputation: 1102

How do I access the value of a node in racket?

I have defined the following node struct:

(struct list-node
  (val next) #:mutable #:transparent)

When I create a list node (list-node 0 #f), however, I don't know how to access the val of this created node. How do I do that?

Upvotes: 0

Views: 109

Answers (1)

user5920214
user5920214

Reputation:

The fields of something defined as (struct <name> (<field-name> ...) ...) are accessed with <name>-<field-name> and (if mutable) set with set-<name>-<field-name>!. There may be options that override these defaults but I don't know them.

So for

(struct list-node
  (val next) #:mutable #:transparent)

Then list-node-val gets the val of a list-node and set-list-node-val! sets it:

(let ([n (list-node 3 #f)])
  (set-list-node-val! n (+ 1 (list-node-val n)))
  (set-list-node-next! n (list-node 6 n))
  n)

Will return a list-node with val 6, and next being another list-node whose next is the original list-node.

Upvotes: 1

Related Questions