[plt-scheme] Re: Re: Typed Scheme

From: Matthew Swank (akopa.gmane.poster at gmail.com)
Date: Sun May 27 01:08:31 EDT 2007

So what are the semantics of case-lambda: when there are multiple cases
with one argument?  It looks like polymorphism is handled using
runtime checks on union types.  This flexible, but since I have to add the
type annotations anyway, it would be be nice if case-lambda: could handle
it:

(define-typed-struct leaf ([val : number]))
(define-typed-struct node ([left : (U node leaf)] [right : (U node leaf)]))

;;maybe  (define: tree-height : ((U node leaf) -> number) 
(define: tree-height : (case-lambda (node -> number) (leaf ->
number))
  (case-lambda:
    (((t : leaf)) 1)
    (((t : node))
     (max (tree-height (node-left t))
          (tree-height (node-right t))))))

Matt

On Sun, 27 May 2007 00:35:37 -0400, Sam TH wrote:

> Matthew-
> 
> Unfortunately not.  If you have a suggestion for a more compact
> syntax, I'd be happy to consider it.
> 
> Also, note that your function always calls the first case, because
> that's how the underlying case-lambda form works.  The fact that your
> code typechecks is actually a bug in the typechecker - that function
> has type (Number -> Number).  The next release of the package will fix
> this (among other) bug.
> 
> sam th
> 
> On 5/27/07, Matthew Swank <akopa.gmane.poster at gmail.com> wrote:
>>
>> 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?
>>

-- 
"You do not really understand something unless you can
 explain it to your grandmother." - Albert Einstein.




Posted on the users mailing list.