[plt-scheme] Newbie: Restricting types

From: C Rose (junk4 at microserf.org.uk)
Date: Fri Mar 19 21:28:57 EST 2004

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 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")?

I'm not trying to start any holy wars here :-)

Thanks

Chris



Posted on the users mailing list.