Scheme 中的手动函数调用
Manual function call in Scheme
(define legal?
(lambda (try legal-pl)
(letrec
((good?
(lambda (new-pl up down)
(cond
((null? new-pl) #t)
(else
(let ((next-pos (car new-pl)))
(and
(not (= next-pos try))
(not (= next-pos up))
(not (= next-pos down))
(good? (cdr new-pl)
(add1 up)
(sub1 down)))))))))
(good? legal-pl (add1 try) (sub1 try)))))
我正在尝试遍历上面的函数。这是我做的手动调用。请看看是不是 in/correct:
(legal? 3 '(1 4 8))
(good? '(1 4 8) 4 2)
(good? '(4 8) 5 1)
(good? '(8) 6 0)
(good? '() 7 -1)
#t
如果不正确,我做错了什么?
这个程序在 DrRacket 的步进器中运行良好。启动 DrRacket,将语言级别设置为 "Intermediate with Lambda",将您的程序(以及对 legal?
的调用)放入缓冲区,然后单击 "step" 按钮。这应该向您展示程序的步骤,一次一个。 (比4步多了很多。)
如果您对此有任何问题,请告诉我。
(define legal?
(lambda (try legal-pl)
(letrec
((good?
(lambda (new-pl up down)
(cond
((null? new-pl) #t)
(else
(let ((next-pos (car new-pl)))
(and
(not (= next-pos try))
(not (= next-pos up))
(not (= next-pos down))
(good? (cdr new-pl)
(add1 up)
(sub1 down)))))))))
(good? legal-pl (add1 try) (sub1 try)))))
我正在尝试遍历上面的函数。这是我做的手动调用。请看看是不是 in/correct:
(legal? 3 '(1 4 8))
(good? '(1 4 8) 4 2)
(good? '(4 8) 5 1)
(good? '(8) 6 0)
(good? '() 7 -1)
#t
如果不正确,我做错了什么?
这个程序在 DrRacket 的步进器中运行良好。启动 DrRacket,将语言级别设置为 "Intermediate with Lambda",将您的程序(以及对 legal?
的调用)放入缓冲区,然后单击 "step" 按钮。这应该向您展示程序的步骤,一次一个。 (比4步多了很多。)
如果您对此有任何问题,请告诉我。