简体   繁体   中英

How do I get a list out of a struct?

I have the given struct :

(define-struct clr ( r g b a)

and I want to have a list out of it:

(list (list r)(list g)(list b)(list a)

My current code:

(define clrTolist
    (lambda (clr)
    (map list (list clr))))

(clrTolist (make-clr 0 0 0 0))

That is the result i'm getting :

list (list (make-clr 0 0 0 0)))

I don't want the structure name to be shown on my end list.

You can "access" single values from struct by using this syntax:

id-field

So in your case

clr-r myred

will return specific value of r for element myred

You want to create a list, consisting of all values of your element so just try

(list (clr-r clr) (clr-g clr) (clr-b clr) clr-a clr))

Make sure to understand structs and its definitions. I recommend this Chapter from HtdP https://htdp.org/2003-09-26/Book/curriculum-ZH-9.html#node_chap_6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM