[racket] typing a function

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Mon Nov 19 12:54:23 EST 2012

On Mon, Nov 19, 2012 at 5:58 PM, Neil Toronto <neil.toronto at gmail.com> wrote:

> This works for me, on 5.3.1:
>
>   (let: ((v : (Vectorof (Array a)) (make-vector first (vector init))))
>     ...)

You mean:

========
#lang typed/racket

(define-type (Array a) (Rec AA (U (Vectorof a) (Vectorof AA))))

(: make-array (All (a) ((Listof Positive-Index) a -> (Array a))))

(define (make-array dims init)
  (define vi (vector init))
  (let loop ((dims dims))
    (match dims
      ((list first)
       (make-vector first init))
      ((list-rest first rest)
       (let: ((v : (Vectorof (Array a)) (make-vector first vi)))
         (for ((i (in-range first)))
           (vector-set! v i (loop rest)))
         v)))))
========

which indeed works, even if with the clumsiness of having to create
and then discard a one-element vector and initialize twice the
non-leaf vectors:

Welcome to DrRacket, version 5.3 [3m].
Language: typed/racket [custom]; memory limit: 128 MB.
> (make-array '(2 3) 0)
- : (Rec g15045 (U (Vectorof Integer) (Vectorof g15045)))
'#(#(0 0 0) #(0 0 0))
> (make-array '(2 3) 'foo)
- : (Rec g15111 (U (Vectorof Symbol) (Vectorof g15111)))
'#(#(foo foo foo) #(foo foo foo))

Thanks!

I will try the array-flatten exercise.

P.

Posted on the users mailing list.