[plt-scheme] macro question
On Jun 10, 2008, at 3:03 PM, Jos Koot wrote:
>>
>>> My personal opinion is that types do not significantly improve
>>> readability of programs. A good similarity between data
>>> structure and the structure of the procedures acting on these
>>> data is far more important, I think (this I did experience, but
>>> not invent by myself, of course)
>>
>> If your comments on data defs and your program organization
>> disagree, which one is right? Who is going to point out the
>> discrepancy? If you had types, the type checker would. That's how
>> types contribute to readability (among other things).
>
> Of course it is nice having a type checker pointing to the
> discrepancy. I agree that explicit formulation of the intentions of
> the programmer can improve readability, but only with the condition
> that the code is not overloaded with an abundance of lines that
> interrupt the story of the algorithms. When possible, I try to
> separate algorithms from data representation. I.e. When I do a
> serious job, I try to write algorithms as abstractions over the
> representation. This is called ADT if I am not mistaken :)
Before we talk too abstractly here, let's look at an example:
UNTYPED
> #lang scheme
>
> ;; An A is one of:
> ;; -- Number
> ;; -- Boolean
> ;; -- (cons A A)
>
> ;; A -> Number
> ;; compute the sum of all numbers in the tree
> (define (Sigma tree)
> (cond
> [(number? tree) tree]
> [(boolean? tree) 0]
> [else (+ (Sigma (car tree)) (Sigma (cdr tree)))]))
>
> (= (Sigma (cons (cons #t 5) 0)) 5)
TYPED:
> #lang typed-scheme
>
> (define-type-alias A (mu A (U Number Boolean (cons A A))))
>
> (: Sigma (A -> Number))
> ;; compute the sum of all numbers in the tree
> (define (Sigma tree)
> (cond
> [(number? tree) tree]
> [(boolean? tree) 0]
> [else (+ (Sigma (car tree)) (Sigma (cdr tree)))]))
>
> (= (Sigma (cons (cons #t 5) 0)) 5)
>
In general, Typed Scheme is as compact as untyped Scheme in HtDP
style. And it is checked for you. -- Matthias