简体   繁体   中英

Access specific list parts in Racket/Scheme

I am having trouble figuring out how to access specific pair information in Racket.

Say I am given some list like this:

(define database-of-components
'((p1 200 (2 p2) (3 p3))
(p2 8 (2 p4))
(p3 60 (1 p5) (2 p6) (3 p7))
(p4 2)
(p5 2)
...
))

How am I able to access the data contained in some individual pn ? Say I am trying to get just 200 from p1 or just 60 from p3 ... how might I do this?

Note: This is obviously for a homework assignment but this question is just to get started.

Look up assq and friends ( assoc , assv ):

> (assq 'p1 database-of-components)
'(p1 200 (2 p2) (3 p3))
> (second (assq 'p1 database-of-components))
200
> (second (assq 'p3 database-of-components))
60

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