从昨天开始,我一直在尝试为方案编写一个特殊情况语句,该语句将执行以下操作:
(define (sort x)
(cond ((and (list? x) x) => (lambda (l)
(sort-list l)))
((and (pair? x) x) => (lambda (p)
(if (> (car p) (cdr p))
(cons (cdr p) (car p))
p)))
(else "here")))
而不是使用所有的and's和cond's语句,我会:
(define (sort x)
(scase ((list? x) => (lambda (l)
(sort-list l)))
((pair? x) => (lambda (p)
(if (> (car p) (cdr p))
(cons (cdr p) (car p))
p)))
(else "here")))
到目前为止,我能做的是:
(define (sort x)
(scase (list? x) (lambda (l)
(sort-list l)))
(scase (pair? x) (lambda (p)
(if (> (car p) (cdr p))
(cons (cdr p) (car p))
p))))
使用此代码:
(define-syntax scase
(syntax-rules ()
((if condition body ...)
(if condition
(begin
body ...)))))
我现在想做的就是允许 scase 语句有多个参数,如下所示:
(scase ((list? (cons 2 1)) 'here)
((list? '(2 1)) 'working))
但我似乎无法弄清楚我该怎么做。也许你们可以给我一点帮助?
提前致谢 ;)