问题描述
我正在和NFA一起进行字符串识别.我有一个宏,它创建一个使用输入的函数,并将其余的传递给其他函数.因为我的NFA图中可能存在循环,所以我正在使用letrec将整个内容放在一起.这是一些代码(已在PLT-Scheme中进行了测试):
I'm just playing with an NFA for string recognition. I have a macro that creates a function which consumes input and passes on the rest to some other functions. Because there might be loops in my NFA graph, I'm using letrec to put the whole thing together. Here is some code (been testing in PLT-Scheme):
(define-syntax-rule (match chars next accepting)
; a function that consumes a list of chars from a list l.
; on success (if there's more to do) invokes each of next on the remainder of l.
(lambda (l)
(let loop ((c chars) (s l))
(cond
((empty? c)
(cond
((and (empty? s) accepting) #t)
(else
(ormap (lambda (x) (x s)) next))))
((empty? s) #f)
((eq? (car c) (car s))
(loop (cdr c) (cdr s)))
(else #f)))))
; matches (a|b)*ac. e .g. '(a a b b a c)
(define (matches? l)
(letrec
([s4 (match '( ) '() #t)]
[s3 (match '(c) `(,s4) #f)]
[s2 (match '(a) `(,s3) #f)]
[s1 (match '( ) `(,s2 ,s5) #f)]
[s5 (match '( ) `(,s6 ,s7) #f)]
[s6 (match '(a) `(,s8) #f)]
[s7 (match '(b) `(,s8) #f)]
[s8 (match '( ) `(,s1) #f)])
(s1 l)))
(matches? '(a c))
(matches? '(a b b b a c))
(matches? '(z a b b b a c))
现在,如果我有一个简单的数据结构来表示我的NFA(如列表),该怎么办.例如
Now, what if I had a simple data-structure to represent my NFA, like a list of lists. e.g.
'((s4 () () #t)
(s3 (c) (s4) #f)
...)
我的问题是:我该如何将该列表转换为以前的letrec语句?我对Macros不太满意,我的理解是我可能不应该使用eval.
My question is: How would I turn that list into the former letrec statement? I'm not too good with Macros and my understanding is that I probably shouldn't be using eval.
推荐答案
如果列表在编译时就已经知道(我的意思是说,是在程序开始运行之前),则可以使用宏.否则,您必须使用eval
.
If the list is known at compile time (what I mean is, before your program starts running) then you can use a macro. Otherwise you must use eval
.
没关系.这是 good 用于评估的一种. :)
It's ok. This is one of the good uses for eval. :)
这篇关于以编程方式填写Scheme中的letrec.宏还是评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!