[英]Instantiate the type of the “rest” parameter of a variadic polymorphic procedure when arity is not known in advance in typed Racket
Let's say I want to transpose a 2 xn "matrix" (list of lists) mat
. 假设我要转置2 xn的“矩阵”(列表列表)
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 在这种情况下,
map
类型为
(All (c a b ...)
(-> (-> a b ... b c) (Listof a) (Listof b) ... b (Listof c)))
Since I'm dealing with a 2 xn matrix, I must instantiate both a and b as Number
: 由于我处理的是2 xn矩阵,因此我必须将a和b都实例化为
Number
:
(apply (inst map (Listof Number) Number Number)
(inst list Number)
mat)
If mat
was a 3 xn matrix, 如果
mat
是3 xn的矩阵,
(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 mxn matrix where m is some unknown positive integer.
现在,假设我正在处理一个mxn矩阵,其中m是一些未知的正整数。 Is there a general way of instantiating map that would work for any value of m?
有没有一种通用的实例化map方法,该方法适用于任何m值?
Thank you Sorawee Porncharoenwase, that pretty much solved my problem. 谢谢Sorawee Porncharoenwase,这几乎解决了我的问题。 Here's what I've done:
这是我所做的:
zip
function as in the link provided: zip
函数: (: zip (∀ (a) (-> (Listof a) (Listof a) * (Listof (Listof a)))))
(define (zip lst . lsts)
(apply map (inst list a) lst lsts))
mat
: mat
: (apply zip (car mat) (cdr mat))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.