1
votes

I'm struggling with the syntax for a function that zips together any number of lists. I currently have:

(define (zip . [lsts : (Listof Any) *]) (apply map (inst list Any) lsts))

Which causes the following error when evaluated:

Error: struct:exn:fail:syntax /Applications/Racket v6.6/collects/racket/private/kw.rkt:929:25: Type Checker: Bad arguments to function in `apply':

Domains: (-> a b ... b c) (Listof a) (Listof b) ... b

(-> a c) (Pairof a (Listof a))

Arguments: (-> Any * (Listof Any)) (Listof (Listof Any)) *

in: (#%app apply map (#%expression list) lsts)

Since these evaluate okay:

(apply map (inst list Any) '(("asd" 1 2) ("cat" 3 4))) ;;(("asd" "cat") (1 3) (2 4))

(define (test . [lsts : (Listof Any) *]) lsts) (test '(1 2 3) '(2 3 "dogs")) ;;((1 2 3) (2 3 "dogs"))

I think the type checker's complaining about apply failing whenever no arguments are passed in, since I get a similar error trying to evaluate the following:

(apply map (inst list Any) '())

Error: struct:exn:fail:syntax /Applications/Racket v6.6/collects/racket/private/kw.rkt:929:25: Type Checker: Bad arguments to function in `apply':

Domains: (-> a b ... b c) (Listof a) (Listof b) ... b

(-> a c) (Pairof a (Listof a))

Arguments: (-> Any * (Listof Any)) Null *

in: (#%app apply map (#%expression list) (quote ()))

But I'm not sure how to specify to the function that it'll take at least one argument (list).

1

1 Answers

4
votes

The function map needs to take at least one list as an argument. Consider what would happen if you called zip with zero arguments. Then you would be calling map with zero lists, which isn't allowed. So, you have to restrict your zip function to take one or more arguments. You can do that by specifying an argument before the rest argument like this:

#lang typed/racket

(define (zip [lst : (Listof Any)] . [lsts : (Listof Any) *])
  (apply map (inst list Any) lst lsts))

One more thing: This would be better if it were polymorphic.

#lang typed/racket

(: zip : (∀ (A) (-> (Listof A) (Listof A) * (Listof (Listof A)))))
(define (zip lst . lsts)
  (apply map (inst list A) lst lsts))

Notice that the domain still needs to be (Listof A) (Listof A) * and not just (Listof A) *.

Update: Even more polymorphism

It's actually possible to make the polymorphism on this even better, so that if you give it exactly 3 lists, it produces a list of exactly-3-element lists. This version of zip would have the type

(: zip : (∀ (A B ...)
            (-> (Listof A)         ; first input
                (Listof B) ... B   ; next n inputs, where n is the number of B type-arguments
                (Listof (List A B ... B)))))

However, if the body were (apply map list lst lsts), the list function would need the type

(∀ (A B ...) (-> A B ... B (List A B ... B)))

However, list used as a function value only has the type (All (a) (-> a * (Listof a))). Instead we can define a new function listd, which behaves exactly like list, but with the new type

;; (listd x y z) = (list x y z)
;; but it's assigned a type that's more convenient for `zip`
(: listd : (∀ (A B ...) (-> A B ... B (List A B ... B))))
(define (listd a . bs)
  (cons a bs))

Using this, the dots-polymorphic version of zip can be defined like this:

(: zip : (∀ (A B ...)
            (-> (Listof A)         ; first input
                (Listof B) ... B   ; next n inputs, where n is the number of B type-arguments
                (Listof (List A B ... B)))))
(define (zip lst . lsts)
  (apply map (inst listd A B ... B) lst lsts))

Using it:

> (zip (list 'a 'b 'c) (list 1 2 3) (list "do" "re" "mi"))
- : (Listof (List (U 'a 'b 'c) Positive-Byte String))
'((a 1 "do") (b 2 "re") (c 3 "mi"))