1
votes

Let's say I want to transpose a 2 x n "matrix" (list of lists) mat. The idiomatic way of doing so in racket is

(apply map list mat)

To do the same thing in typed/racket, I have to help the type checker a little. The type of map in that case is

(All (c a b ...) 
  (-> (-> a b ... b c) (Listof a) (Listof b) ... b (Listof c)))

Since I'm dealing with a 2 x n matrix, I must instantiate both a and b as Number:

(apply (inst map (Listof Number) Number Number) 
       (inst list Number)
       mat)

If mat was a 3 x n matrix,

(apply (inst map (Listof Number) Number Number Number)
       (inst list Number)
       mat)

Would do the trick. Now, let's say that I'm dealing with an m x n matrix where m is some unknown positive integer. Is there a general way of instantiating map that would work for any value of m?

1
I think this is exactly what you want stackoverflow.com/a/44864244/718349Sorawee Porncharoenwase

1 Answers

0
votes

Thank you Sorawee Porncharoenwase, that pretty much solved my problem. Here's what I've done:

  1. Define a polymorphic zip function as in the link provided:
(: zip (∀ (a) (-> (Listof a) (Listof a) * (Listof (Listof a)))))
(define (zip lst . lsts)
  (apply map (inst list a) lst lsts))
  1. Apply it to mat:
(apply zip (car mat) (cdr mat))