我正在尝试为莫尔斯电码编写解码器。到目前为止,我已经创建了一个包含所有字母的摩尔斯电码的树。
(define gap '/)
(define long-gap '_)
(define long '-)
(define short '*)
(define morsetree
(make-node "-" gap
(make-node "E" short
(make-node "I" short
(make-node "S" short
(make-node "H" short empty empty)
(make-node "V" long empty empty))
(make-node "U" long
(make-node "F" short empty empty)
empty))
(make-node "A" long
(make-node "R" short
(make-node "L" short empty empty)
empty)
(make-node "W" long
(make-node "P" short empty empty)
(make-node "J" long empty empty))))
(make-node "T" long
(make-node "N" short
(make-node "D" short
(make-node "B" short empty empty)
(make-node "X" long empty empty))
(make-node "K" long
(make-node "C" short empty empty)
(make-node "Y" long empty empty)))
(make-node "M" long
(make-node "G" short
(make-node "Z" short empty empty)
(make-node "Q" long empty empty))
(make-node "O" long empty empty)))))
我有一个可用于单个字符的解码器:
(define (decode-character code atree)
(cond
((symbol=? (first code) '*) (decode-character (rest code)(node-left atree)))
((symbol=? (first code) '-) (decode-character (rest code)(node-right atree)))
((symbol=? (first code) '/) (node-letter atree))))
但是我的文字解码器无法正常工作:
(define (decode code atree)
(cond
((symbol=? (first code) '*) (decode (rest code)(node-left atree)))
((symbol=? (first code) '-) (decode (rest code)(node-right atree)))
((symbol=? (first code) '/) (cons (node-letter atree) (decode (rest code) atree)))
((and (symbol=? (first code) '_) (empty? (rest code)))
(cons (node-letter atree) (cons " " empty)))
((and (symbol=? (first code) '_) (not(empty? (first (rest code))))) (cons (node-letter atree) (cons " " (decode (rest code) atree))))))
使用这个测试: (decode (list '- '/ '* '* '/ '* '_) morsetree)
给我 (list "T" "D" "B" " ") 而不是 (list "T" "I" "E" " ") 因为解码器停留在树中它停止的位置。因此,解码读取的不是 '* '* '- '* '*。
成功解码一封信后,如何“跳转”到我的 morsetree 的开头?
对于这样一个小问题,有很多文字。我不需要一个代码,只是一个很好的提示我如何解决这个问题。提前致谢。