2
相關的所有值我有一個列表像(列表(列表鍵值)...),我試圖建立一個函數返回與鍵相關的所有值。racket:我試圖建立一個函數,返回與鍵
我想我可以使用匹配構造來做到這一點,但沒有任何成功。
#lang racket
; should return a list starting with key, otherwise #f
(define (match-key lst key)
(match lst
[(list key val) value]
[_ #f]))
; testing data
(define test-lst
(list
(list 'title "Lorem title")
(list 'price 999.99)
(list 'colour "red")
))
(eq? "Lorem title" (match-key test-lst 'title)) ; should return #t
(eq? "Another lorem" (match-key test-lst 'title)) ; should return #f
(eq? 999.99 (match-key test-lst 'price)) ; should return #t
(eq? 111.11 (match-key test-lst 'price)) ; should return #f