我的scheme函数有问题,它试图用给定的语法创建生产值



因此,我正在尝试创建一个函数,其中给定以下输入'((a (xz) (c)) (b (wy) (d)))我应该做一些类似'((a (xz)) (a (c)) (b (wy)) (b (d)))的事情

我试着写这个

(define productionValues
(lambda (input)
(let ((lhs (map (lambda (x) (car x)) input)))
(let ((rhs (map (lambda (y) (cdr y)) input)))
(map (lambda (l) (cons l (map (lambda (r) (car r)) rhs))) lhs)
)
)
))

这不起作用,使我获得((a (xz) (c)) (b (xz) (c)))我的逻辑是,我有一个存储(a b)的变量lhs和存储(((xz) (c)) ((wy) (d)))的变量rhs

我不完全确定你想在那里完成什么,但这产生了所需的输出:

(define (productionValues input)
; flatten the sublists
(apply append
; create the lists as per the sample
(map (lambda (x) (list (list (first x) (second x))
(list (first x) (third  x))))
input)))

例如:

(define input '((a (xz) (c)) (b (wy) (d))))
(productionValues input)
=> '((a (xz)) (a (c)) (b (wy)) (b (d)))

最新更新