我无法定义一个可以与Racket中的拉链相同功能的功能。到目前为止,我有这个:
(define (zipper lst1 lst2)
(match* [lst1 lst2]
[{'()'()} '()]
[{(cons hd1 tl1) (cons hd2 tl2)}
(cons (list hd1 d2)
(zipper tl1 tl2))]))
有人可以解释我在这里出错的地方。我希望它看起来像这样:
> (zipper '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
答案 0 :(得分:5)
您可以使用map
与list
结合使用此功能。
> (map list '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
因此,如果你想让它成为单个功能拉链,它看起来像:
(define (zipper . args)
(apply map list args))
此功能与map list
:
> (zipper '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
答案 1 :(得分:3)
你非常接近:
(define (zipper lst1 lst2)
(match* [lst1 lst2]
[{'() '()} '()]
[{(cons hd1 tl1) (cons hd2 tl2)}
(cons (list hd1 hd2)
(zipper tl1 tl2))]))
我将d2
更改为hd2
。