[plt-scheme] Re: Typed Scheme
On Sat, 26 May 2007 07:57:12 -0400, Sam TH wrote:
> On 5/25/07, Matthew Swank <akopa.gmane.poster at gmail.com> wrote:
>>
>> Could someone post an example using the (All (v ...) t) construct.
>
> Here's a simple example:
>
> (define: simple-map : (All (a) ((a -> a) (Listof a) -> (Listof a)))
> (plambda: (a) ([f : (a -> a)] [l : (Listof a)])
> (cond [(null? l) null]
> [else (cons (f (car l))
> (map f (cdr l)))])))
>
> (simple-map (lambda: ([x : Number]) (+ x 1)) '(1 2 3))
>
> However, this can be more concisely written as follows:
>
> (pdefine: (a) (simple-map [f : (a -> a)] [l : (Listof a)]) : (Listof a)
> (cond [(null? l) null]
> [else (cons (f (car l))
> (map f (cdr l)))]))
>
> where pdefine: abbreviates all of the boilerplate, including the use
> of (All ...).
>
This is a silly example:
(define: x-or-nil : (case-lambda (number -> number)
((Listof number) -> (Listof number)))
(case-lambda:
(((x : number)) x)
(((x : (Listof number))) ())))
but is there a more compact way to define case-lambdas?
Thanks again,
Matt
--
"You do not really understand something unless you
can explain it to your grandmother." - Albert Einstein.