对哈希表中的值进行排序,并在scheme中提取密钥

对哈希表中的值进行排序,并在scheme中提取密钥,scheme,hashtable,racket,Scheme,Hashtable,Racket,我对这个计划还不熟悉,并试图在这方面占优势。我有一个哈希表。我想对哈希表的值进行排序,然后使用它检索值最小的键。我有一个列表的选择排序代码。但我不知道如何对哈希映射中的值进行排序,然后提取值最低的键。有人能告诉我一些关于这件事的细节吗。这是我到目前为止所拥有的 (define (dirs-last-modified list) (define ht (make-hash)) (cond [(null? list) '()] [else (hash-set! (first

我对这个计划还不熟悉,并试图在这方面占优势。我有一个哈希表。我想对哈希表的值进行排序,然后使用它检索值最小的键。我有一个列表的选择排序代码。但我不知道如何对哈希映射中的值进行排序,然后提取值最低的键。有人能告诉我一些关于这件事的细节吗。这是我到目前为止所拥有的

(define (dirs-last-modified list)
  (define ht (make-hash))
   (cond
    [(null? list) '()]
    [else (hash-set! (first list) (file-or-directory-modify-seconds (first list))) (dirs-last-modified (rest list))])
 )
(define (selection list) 
 (cond ( (null? list) '() )
     ( else (cons (smallest list (car list))     ; put the smallest element
                                           ; at the front of the 
                                           ; current list 
                  (selection (remove list (smallest list (car list)))))
                                           ; call selection on the list
                                           ; minus the smallest
                                           ; element
     )
  )
 )

(define (remove list A)                ; remove the first occurance of atom A from list
 (cond ( (null? list) '() )           
    ( (= (car list) A) (cdr list))    ; Match found! 
    (else (cons (car list)(remove (cdr list) A)))   ; keep searching
  )
 )

(define (smallest list A)             ; looks for the smallest element in the list
                               ; atom A is the current smallest
 (cond ( (null? list) A)
    ( (< (car list) A) (smallest (cdr list)(car list)))
    (else (smallest (cdr list) A ))
 )
)
(定义(目录上次修改列表)
(定义ht(生成哈希))
(续)
[(空?列表)'()]
[否则(哈希集!(第一个列表)(文件或目录修改秒数(第一个列表))(上次修改的目录数(剩余列表)))
)
(定义(选择列表)
(cond((空?列表)“”())
(else(cons(最小列表(汽车列表));放入最小元素
;在前面
;当前列表
(选择(删除列表(最小列表(车辆列表'))))
;列表中的呼叫选择
;减去最小的
;元素
)
)
)
(定义(删除列表A);从列表中删除原子A的第一次出现
(cond((空?列表)“”())
(=(汽车列表)A)(cdr列表));找到匹配项!
(其他(cons(车辆列表)(删除(cdr列表)A));继续搜索
)
)
(定义(最小列表A);查找列表中最小的元素
;原子A是目前最小的
(条件((空?列表)A)
(<(车辆列表)A)(最小的(cdr列表)(车辆列表)))
(其他(最小的(cdr列表)A))
)
)

无需对值进行排序,我们可以提取最小值,然后找到相应的键。以下是Racket中检索哈希表中最小值对应的键的惯用方法:

(define (key-min-value ht)
  (car                 ; extract the key of the minimum entry
   (argmin             ; find the minimum in the entry list
    cdr                ; using as criterion the value in each entry
    (hash->list ht)))) ; convert hash table into key-value list
例如:

(key-min-value '#hash((c . 3) (a . 1) (b . 2)))
=> 'a