JJ12
JJ12

Reputation: 29

How to make a list out of a structure in racket?

I made an structure (struct g( a b c d)) and I define it as (define per (g 1 6 5 4))

however I want to use per as a list argument for another function I want to define.. it gives me an error

first: contract violation expected: (and/c list? (not/c empty?))
given: #

how can I make per a list with arguments of a structure? or there is no other ways of making something similar.

Upvotes: 0

Views: 2075

Answers (2)

Shawn
Shawn

Reputation: 52674

If it's a transparent or prefab struct, you can use struct->list to convert it to a list:

> (struct g (a b c d) #:transparent)
> (define foo (g 1 6 5 4))
> (struct->list foo)
'(1 6 5 4)

Upvotes: 0

Sylwester
Sylwester

Reputation: 48775

When you are making a struct it's very much like objects in other languages. You have a struct "g" with 4 named slots. Eg. you access the first with (g-a struct-var) and so on.

If you want a list then make a list. If you need to make a list from a g you need to do something like (list (g-a x) (g-b x) (g-c x) (g-d x)) and to do the opposite you do (apply g lst)

Upvotes: 1

Related Questions