◀ Previous Next ▶ Up ▲


witpoko.com


ถามผู้รู้เรื่อง Scheme key-value pair ครับ

อยากทราบวิธีออกแบบอัลกอรึทึมโดยโปรแกรม ดร.สครีม ครับ
; map abstract data type.
; stores (key, value) pairs where key is unique
; all functions use equals? in testing for
; equality of keys.

; function: (newmap)
; create and return new, empty map

; function: (mapadd m key value)
; add key/value pair to map m
; if m already contains a pair with that key,
; the old value is replace by the new value.
; Returns: unspecified

; function: (mapget m key)
; If map m contains a key/value pair with the given key,
; returns a pair p where (car p) is the key and (cdr p) is the value.
; Returns #f if the map does not contain a pair with the given key.

; function: (mapremove m key)
; remove key/value pair with given key from map m.
; if m does not contain a pair with the given key,
; no action is taken.
; Returns: unspecified
The following shows a sample run of the code:
> (define m (newmap)) ; create map
> (mapget m 'james); ; shouldn’t be there
#f
> (mapadd m 'james "professor") ; use symbol key, string value
> (mapadd m "KMUTT" 'cpe) ; use string key, symbol value
> (mapget m 'james)
(james . "professor")
> (mapget m "KMUTT")
("KMUTT" . cpe)
> (mapget m 'KMUTT) ; shouldn’t be there
#f
> (mapadd m 'james 'teacher) ; should replace old value
> (mapget m 'james)
(james . teacher)
> (mapremove m "KMUTT")
> (mapget m "KMUTT")
#f


จากคุณ : เอกสิทธิ์ -[ 11 ก.พ. 49 - 17:58:01 A:61.91.116.2 X: ]





กระทู้ยอดนิยม