I'm having trouble define a function that can do the same functionalities as zipper in Racket. So far I have this:
(define (zipper lst1 lst2)
(match* [lst1 lst2]
[{'()'()} '()]
[{(cons hd1 tl1) (cons hd2 tl2)}
(cons (list hd1 d2)
(zipper tl1 tl2))]))
Can someone explain where I'm going wrong here. I want it to look like this:
> (zipper '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
You can use map
combined with list
to have this functionality.
> (map list '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
So, if you want to make it a single function zipper, it would look something like:
(define (zipper . args)
(apply map list args))
This function works the same as map list
:
> (zipper '(1 2 3 4) '(a b c d))
'((1 a) (2 b) (3 c) (4 d))
You were pretty close:
(define (zipper lst1 lst2)
(match* [lst1 lst2]
[{'() '()} '()]
[{(cons hd1 tl1) (cons hd2 tl2)}
(cons (list hd1 hd2)
(zipper tl1 tl2))]))
I changed d2
to hd2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With