矢量地图的 ikarus 实现
这段代码位于 Ikarus 的 vector-map
实现中:
(let f ([p p] [v v] [i 0] [n (vector-length v)] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v ($fxadd1 i) n (cons (p (vector-ref v i)) ac))]))]
为什么命名的 let 包含参数 p
、v
和 n?
vector-map
的完整定义如下。
(module (vector-map)
(define who 'vector-map)
(define (ls->vec ls n)
(let f ([v (make-vector n)]
[n n]
[ls ls])
(cond
[(null? ls) v]
[else
(let ([n ($fxsub1 n)])
($vector-set! v n ($car ls))
(f v n ($cdr ls)))])))
(define vector-map
(case-lambda
[(p v)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v)
(die who "not a vector" v))
(let f ([p p] [v v] [i 0] [n (vector-length v)] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v ($fxadd1 i) n (cons (p (vector-ref v i)) ac))]))]
[(p v0 v1)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v0)
(die who "not a vector" v0))
(unless (vector? v1)
(die who "not a vector" v1))
(let ([n (vector-length v0)])
(unless ($fx= n ($vector-length v1))
(die who "length mismatch" v0 v1))
(let f ([p p] [v0 v0] [v1 v1] [i 0] [n n] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v0 v1 ($fxadd1 i) n
(cons (p ($vector-ref v0 i) ($vector-ref v1 i)) ac))])))]
[(p v0 v1 . v*)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v0)
(die who "not a vector" v0))
(unless (vector? v1)
(die who "not a vector" v1))
(let ([n (vector-length v0)])
(unless ($fx= n ($vector-length v1))
(die who "length mismatch" v0 v1))
(let f ([v* v*] [n n])
(unless (null? v*)
(let ([a ($car v*)])
(unless (vector? a)
(die who "not a vector" a))
(unless ($fx= ($vector-length a) n)
(die who "length mismatch")))
(f ($cdr v*) n)))
(let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v0 v1 v* ($fxadd1 i) n
(cons
(apply p ($vector-ref v0 i) ($vector-ref v1 i)
(let f ([i i] [v* v*])
(if (null? v*)
'()
(cons ($vector-ref ($car v*) i)
(f i ($cdr v*))))))
ac))])))])))
This bit of code is in Ikarus' implementation of vector-map
:
(let f ([p p] [v v] [i 0] [n (vector-length v)] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v ($fxadd1 i) n (cons (p (vector-ref v i)) ac))]))]
Why does the named let include the parameters p
, v
, and n
?
The full definition of vector-map
follows.
(module (vector-map)
(define who 'vector-map)
(define (ls->vec ls n)
(let f ([v (make-vector n)]
[n n]
[ls ls])
(cond
[(null? ls) v]
[else
(let ([n ($fxsub1 n)])
($vector-set! v n ($car ls))
(f v n ($cdr ls)))])))
(define vector-map
(case-lambda
[(p v)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v)
(die who "not a vector" v))
(let f ([p p] [v v] [i 0] [n (vector-length v)] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v ($fxadd1 i) n (cons (p (vector-ref v i)) ac))]))]
[(p v0 v1)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v0)
(die who "not a vector" v0))
(unless (vector? v1)
(die who "not a vector" v1))
(let ([n (vector-length v0)])
(unless ($fx= n ($vector-length v1))
(die who "length mismatch" v0 v1))
(let f ([p p] [v0 v0] [v1 v1] [i 0] [n n] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v0 v1 ($fxadd1 i) n
(cons (p ($vector-ref v0 i) ($vector-ref v1 i)) ac))])))]
[(p v0 v1 . v*)
(unless (procedure? p)
(die who "not a procedure" p))
(unless (vector? v0)
(die who "not a vector" v0))
(unless (vector? v1)
(die who "not a vector" v1))
(let ([n (vector-length v0)])
(unless ($fx= n ($vector-length v1))
(die who "length mismatch" v0 v1))
(let f ([v* v*] [n n])
(unless (null? v*)
(let ([a ($car v*)])
(unless (vector? a)
(die who "not a vector" a))
(unless ($fx= ($vector-length a) n)
(die who "length mismatch")))
(f ($cdr v*) n)))
(let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n] [ac '()])
(cond
[($fx= i n) (ls->vec ac n)]
[else
(f p v0 v1 v* ($fxadd1 i) n
(cons
(apply p ($vector-ref v0 i) ($vector-ref v1 i)
(let f ([i i] [v* v*])
(if (null? v*)
'()
(cons ($vector-ref ($car v*) i)
(f i ($cdr v*))))))
ac))])))])))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说清楚为什么,因为它看起来确实是不必要的,但可能这是一种减少引用的自由变量数量的优化。在这种情况下,
p
、v
和n
成为f
的词法变量,并且不再需要自由变量参考。不过,由于对应的自由变量实际上是
vector-map
的词法变量并且没有进一步修改,所以编译器在内部自动进行这种优化应该不难。现在,如果 Ikarus 没有编译器,那么这可能可以解释手动优化。It's hard to tell why, since it does appear to be unnecessary, but probably it's an optimisation to reduce the number of free variables that are referenced. In this case,
p
,v
, andn
become lexical variables off
, and no longer require free variable references.However, since the corresponding free variables are actually lexical variables of
vector-map
and not further modified, it shouldn't be hard for the compiler to automatically do this kind of optimisation internally. Now, if Ikarus doesn't have a compiler, then that might explain the manual optimisation.