我正在用球拍和博士学习NLP。
我正在处理以下代码:
#lang racket
(define english-1
'((Initial (1))
(Final (9))
(From 1 to 3 by NP)
(From 1 to 2 by DET)
(From 2 to 3 by N)
(From 3 to 4 by BV)
(From 4 to 5 by ADV)
(From 4 to 5 by |#|)
(From 5 to 6 by DET)
(From 5 to 7 by DET)
(From 5 to 8 by |#|)
(From 6 to 7 by ADJ)
(From 6 to 6 by MOD)
(From 7 to 9 by N)
(From 8 to 8 by MOD)
(From 8 to 9 by ADJ)
(From 9 to 4 by CNJ)
(From 9 to 1 by CNJ)))
(define (getf x y)
(if (eq? (car x) y)
(cadr x)
(getf (cdr x) y)))
(define (initial-nodes network)
(list-ref (assoc 'Initial network) 1))
(define (final-nodes network)
(list-ref (assoc 'Final network) 1))
(define (transitions network)
(filter (lambda (x) (eq? (car x) 'From)) network))
(define (trans-node transition)
(getf transition 'From))
(define(trans-newnode transition)
(getf transition 'to))
(define (trans-label transition)
(getf transition 'by))
(define abbreviations
'((NP kim sandy lee)
(DET a the her)
(N consumer man woman)
(BV is was)
(CNJ and or)
(ADJ happy stupid)
(MOD very)
(ADV often always sometimes)))
(define (recognize network tape)
;; returns t if sucessfully recognizes tape - nil otherwise
(call/cc (lambda (return)
(define (recognize-next node tape network)
(if (and (null? tape) (member node (final-nodes network)))
(return #t) ; success
(for ([transition (transitions network)])
;; try each transition of the network
(when (equal? node (trans-node transition)) ; if it starts at the right node
(for ([newtape (recognize-move (trans-label transition) tape)])
;; try each possible new value of tape
(recognize-next (trans-newnode transition) newtape network))))))
(for ([initialnode (initial-nodes network)])
(recognize-next initialnode tape network))
null))) ; failed to recognize
(define (recognize-move label tape)
(if (or (eq? label (car tape))
(member (car tape) (assoc label abbreviations)))
(list (cdr tape))
(if (eq? label '|#|)
(list tape)
null)))
(require racket/trace)
(trace recognize-move)
(recognize-move english-1 '(hahaha))代码似乎大部分都没问题。但是,我一直收到一个与识别-移动函数相关的错误消息:
member: not a proper list: #f我以为我在处理名单..。我怎么才能解决这个问题?
发布于 2016-10-25 13:33:23
问题在于这种形式:
(member (car tape) (assoc label abbreviations))如果assoc找不到任何东西,结果就是#f。(member 'anything #f)将无法工作。在公共项中,Lisp与空列表相同,因此在false上的member将有效,但在Scheme中不起作用。您也许可以确定这是一个这样的列表:
(member (car tape) (or (assoc label abbreviations) '()))发布于 2016-10-25 13:44:39
这是从Common翻译过来的代码。在CL中,nil是false,也是空列表()。在球拍中,#f是假的,与()不一样。如果没有找到匹配项,assoc希望返回false :由于CL对false的双关语和空列表的方式,这意味着(member ... (assoc ...))将始终工作。在球拍中,它不会:你需要检查看看assoc是否找到匹配。
https://stackoverflow.com/questions/40240929
复制相似问题