Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define zipper function in Racket

Tags:

racket

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))
like image 285
tadashi Avatar asked Jan 05 '23 18:01

tadashi


2 Answers

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))
like image 189
Leif Andersen Avatar answered Jan 08 '23 09:01

Leif Andersen


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.

like image 22
soegaard Avatar answered Jan 08 '23 09:01

soegaard