I'm pretty new to racket and I'm trying to make a finite state machine for a project. here's what I have so far.
#lang racket
(define (test rules cs pass lst)
(if (eq? (length lst) 0)
(if (member cs pass)
(write 'passed)
(write 'failed))
(test rules (next rules cs (car lst)) (cdr lst))))
(define (next rules cs input)
(if (eq? cs (car (car rules)))
(if (eq? input (car(cdr(car rules))))
(car(cdr(cdr(car rules))))
((next (cdr rules) cs input)))
(next (cdr rules) cs input)))
then I input
(define rules '('(0 a 0) '(0 b 1) '(1 a 0) '(1 b 1)))
(define pass '(1))
(test rules 0 pass '(a b a b))
and for a reason that I cannot figure out, I get this error on the next function. It acts as though rules is an empty list, which I'm pretty sure it's not. Anyway if anyone could tell me why it's doing that it would really help.
Thanks!
EDIT1
Thanks so much for the help guys. It's really helping. The code is working entirely until the end, but now it throws an error at the end of my code if I test multiple lists with testmany.
(define (testmany rules cs pass lst)(if (eq? (length lst) 0)
(write 'done)
((test rules cs pass (car lst))
(testmany rules cs pass (cdr lst)))))
(define (test rules cs pass lst) (if (eq? (length lst) 0)
(if (member cs pass)
(write 'accepted.....)
(write 'rejected.....))
(test rules (next rules cs (car lst)) pass (cdr lst))))
(define (next rules cs input) (if (eq? cs (car (car rules)))
(if (eq? input (car(cdr(car rules))))
(car(cdr(cdr(car rules))))
(next (cdr rules) cs input))
(next (cdr rules) cs input)))
The code doesn't seem to know when to stop the recursion of the testmany function. When the list is empty, it prints done, but then continues the next part as if the list still had elements, throwing this error. If anyone knows why it is throwing this error please let me know. Thanks!
(define rules ...)
, see What is the difference between quote and list? and its answers. When in doubt, just uselist
, it's simpler. – Alex Knauth((next...)
means that thenext
function will return a function that you immediately call. If the returned value is anything else you'll get a runtime error fromapply
.'('(0 a 0) ...)
evaluates to((quote (0 a 0)) ...)
since only the outer quote is code, the rest became data. – Sylwester(member cs pass)
evaluates to a list there will be a crash. To fix this you would need to put(boolean? (member cs pass
and switch the if statement arguments to 'failed then 'passed as to keep it consistent with this example. The last line of the(test ...)
function has too less arguements. test requires 4 and only 3 has been passed – Martin Watson