简体   繁体   中英

Get second value from a list of structure in racket?

Suppose a list in racket contains (cons (make-card 'spades 8) (cons (make-card 'hearts 3) empty))

  (define-struct card(suit value))

  (card-suit(first(cons (make-card 'spades 8) (cons (make-card 'hearts 3) empty))))
  --> give me 'spades
  what to do to get hearts

If i write

 (rest(first(cons (make-card 'spades 8)(cons (make-card 'hearts 3) empty))))

I am getting error

rest: expects a non-empty list; given: (make-card 'spades 8)

As @melpomene says, you have your first and rest reversed.

The error says that rest was given (make-card 'spades 8) , which is the first item in your list. Instead you want to get the rest of your list (so you have a list that starts with (make-card 'hearts 3) ) and then take the first of that (to get the card at the start of that shorter list).

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