[racket] Local form in typed racket
`define:' also works. Its syntax is a bit different from what you had,
though. Each argument is annotated with its type inside the function
header, and the return type of the function is after the `:'.
(: 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 : (Listof EmeType)] [remaining : Integer])
: (Listof EmeType)
(cond [(< remaining 1) lst]
[else (bld-lst (cons (new-emetype) lst)
(- remaining 1))])])
(bld-lst '() how-many-types)))
Also, you don't actually need `local' here. Racket (and Typed Racket),
unlike Racket's teaching languages, allow local definitions directly
inside `define'.
If you prefer to write down the entire function type all in one place,
you can also use the `:' form, which also works for internal definitions
(even inside local):
(: make-random-emetype-list (Range -> (Listof EmeType)))
(define (make-random-emetype-list range)
(define how-many-types (random-integer (Range-lower range)
(Range-upper range)))
(: bld-lst ((Listof EmeType) Integer -> (Listof EmeType)))
(define (bld-lst lst remaining)
(cond [(< remaining 1) lst]
[else (bld-lst (cons (new-emetype) lst)
(- remaining 1))]))
(bld-lst '() how-many-types))
Vincent
At Sat, 9 Feb 2013 20:07:09 -0800,
Ben Dean wrote:
> As so often happens when one is forced to type out a question in real
> words, shortly after sending the previous email, I was able to answer my
> own question.
>
> the result:
>
> (: 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 '())))
>
> Thanks!
>
>
> On Sat, Feb 9, 2013 at 6:56 PM, Ben Dean <benjamin.dean at aya.yale.edu> wrote:
>
> > It would be very helpful to me to have an example of the "local" form for
> > defining an internal function in typed racket. I've read and re-read the
> > docs and can't seem to figure it out from the syntax definition.
> >
> > Here's what i've been trying:
> >
> > (: 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)))
> >
> > but I get an error on this: "define:: expected the identifier `:' in:
> > ((Listof EmeType) Integer -> (Listof EmeType))"
> >
> > Thanks for the help!
> >
> > Ben
> >
> [1.2 <text/html; ISO-8859-1 (quoted-printable)>]
>
> [2 <text/plain; us-ascii (7bit)>]
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users