[plt-scheme] Newbie: Restricting types

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Mar 19 21:49:50 EST 2004

On Mar 19, 2004, at 9:28 PM, C Rose wrote:

>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hi all
>
> I'm new to Scheme and am working through the "How to Design Programs" 
> book. I'm learning Scheme for interest only at this stage and I'm 
> trying to reconcile some of the differences between Scheme and other 
> languages I'm familiar with (mostly imperative languages with C-style 
> syntaxes).
>
> I'm intrigued by the dynamic typing in Lisp and Scheme (particularly 
> in the face of comments like "C/C++/Java/Perl/etc. are for people who 
> want to make things that work. Common Lisp is for peple who want to 
> make things that don't break" by Lisp/Scheme advocates). Coming from 
> such a background, I'm a little bemused at how dynamic typing can 
> facilitate more robust programs.

I doubt you will find these words in the book; I am pretty sure though 
at my age you never know :-)

You need to learn to distinguish C++ from Java. The former has an 
unsound type system, the latter a sound one. And Perl doesn't have one 
for sure. These are huge differences and a true understanding requires 
a lot of work. A lot.

> I understand that one can gain flexibility by defining functions that 
> take arguments of arbitrary type. I've looked at structures in Scheme, 
> and I can see that one can write a function that is essentially 
> polymorphic by explicitly testing the type(s) of the arguments, for 
> example (from HtDP):
>
> (define (perimeter a-shape)
>   (cond
>     [(circle? a-shape)
>      (* (* 2 (circle-radius a-shape)) pi)]
>     [(square? a-shape)
>      (* (square-length a-shape) 4)]))
>
> Imagine that I have a structure defined as follows:
>
> (define-struct three-vector (x y z))
>
> I can create a three-vector of numbers by doing (make-three-vector 1 2 
> 3). I can also create a three-vector of symbols by doing 
> (make-three-vector 'a 'b 'c). However, is it possible to write the 
> definition of three-vector so that one cannot create a three-vector 
> with non-number entries? If this is not the correct thing to want to 
> do, then why?
>
> More generally, can someone explain how the dynamically-typed approach 
> can yield more robust programs than the statically-typed approach 
> (without assuming the programmer is always going to be wise enough to 
> place guards on the types that their functions will accept -- this 
> seems to not only be wildly optimistic, but a kind of "static typing 
> in reverse")?

In section 7 you learn to write "protected" functions. In 'real' 
Scheme, we can also express these things via 'real' contracts. So you 
could, for example, write a module like this:

(module 3vector mzscheme
   (require (lib "contract.ss"))

   (define-struct 3vec (x y z))
   (provide/contract (struct 3vec ((x number?) (y number?) (z 
number?)))))

And then import this module in another one:

(module another mzscheme
   (require 3vector)

   (make-3vec 'a 3 'b))

Put this into PLT Pretty Big and execute. Then watch what happens. Now 
change this so that you can build vectors of only odd numbers. And oh, 
do that also in your favorite statically typed language.

-- Matthias





Posted on the users mailing list.