本地定义类型球拍的用法

时间:2013-02-10 02:22:22

标签: scheme racket typed-racket

我很难弄清楚如何在Typed Racket中使用本地表格。

这有什么问题:

(: make-random-emetype-list (Range -> (Listof EmeType)))
(define (make-random-emetype-list range )
 (local ([define how-many-types (random-integer (Range-lower range) (Range-upper range))]
         [define: (bld-lst  lst remaining)  : ((Listof EmeType) Integer -> (Listof EmeType))
           (cond [(< remaining 1) lst]
                 [else (bld-lst (cons (new-emetype) lst) (- remaining 1))])])          
(bld-lst '() how-many-types)))

我确定问题在于我并没有完全遵循ts-reference中的语法规范,但在类型化的球拍中有一个或两个本地定义的函数会非常有用。

1 个答案:

答案 0 :(得分:1)

答案是,使用letrec:。好吧,现在我知道了。

(: make-random-emetype-list (Range -> (Listof EmeType)))
(define (make-random-emetype-list range )
  (letrec: ([lower : Integer (Range-lower range)]
        [upper : Integer (Range-upper range)]
        [how-many : Integer (random-integer lower upper)]
        [bld-lst : (Integer (Listof EmeType) -> (Listof EmeType)) (lambda: ([remaining : Integer] [lst : (Listof EmeType)]) 
                                                                    (cond [(< remaining 1 ) lst]
                                                                          [else (bld-lst (- remaining 1) (cons (new-emetype) lst))]))])
    (bld-lst how-many '())))