Needless copying in iterations?

Paul Rubin http
Sun Sep 16 12:12:56 EDT 2007


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> > What do you mean by Haskell is a dynamic language?  It is statically and
> > strict typed and the compiler usually knows all the functions.  No
> > "surprises", no side effects, no duck typing.
> 
> Haskell's IO monad (and possibly the do monad?) allows side effects. It 
> would be a pretty poor programming language that didn't allow input or 
> output!

In a certain theoretical sense, input and output in the IO monad are
not side effects.  Side effects means you can somehow distinguish
between calling f(x) twice with the same value of x, and calling it
just once.  In Haskell, the print function does an output operation
(called an "action") but if I understand it right, it turns out you
cannot call the print function twice with the same argument.  Of
course you can say
   main = do { print "hello"; print "hello" }
and that prints "hello" twice, but the do statement is syntactic sugar
that turns the above into approximately like (the exact mechanism is
a bit hairier):
    x = (special token representing the state of the external world)
    y = print ("hello", x)  # y is the new external world state
    z = print ("hello", y)  # z is another new state
so print receives a different arg each time.  (Note that every Haskell
function has exactly one argument, so f(x,y) means call f, giving it
the tuple (x,y) as an argument).  

> See also http://homepages.cwi.nl/~ralf/OOHaskell/
> showing that Haskell can do object-oriented programming, complete with 
> mutable objects and side-effects. Although "duck typing" is listed as a 
> keyword, I couldn't see any reference to it in the paper.

That describes an OO extension to Haskell, which I suppose is
interesting, but it's not Haskell.  You could also bolt a Python
interpreter into Haskell and that would not be Haskell either.

> Haskell uses type inference, and has a "maybe" type for those cases where 
> it can't tell what the type will be.

Type inference just means the static type system doesn't rely on
declarations.  Like when you say 3+4 in C++ or Java, the compiler
figures out that is an integer expression, but if you say 
  x = 3 + 4
without an "int x" declaration, the compiler complains.  Haskell goes
a little further and lets you say
   x = 3 + 4
and the compiler says "ok, now I know that x is an integer".  If you
later try to concatenate x with a string:
   y = x ++ "hello"
you get a compile time error message.  In a dynamic language like
Python you would not get the error message til runtime.  That is the
difference between a static and dynamic language.
   
The Maybe monad is still a static type.  You can think of it as a
statically typed list with a maximum of one element, i.e. a Maybe Int
can have values like [3] (denoted "Just 3") or like [] (the empty
list, denoted Nothing).  It cannot have a value like "foo" or
[3.14159] or [[7]] or [2,3,4].  You get compile time error messages
if you try to assign those values to it.  Of course you can have a
Maybe Float (like [3.14159]) or a Maybe (Maybe Int) (like [[7]]).

> If you still don't accept that Haskell is a dynamic language, for 
> whatever definition of dynamic language you use, I'll withdraw the claim 
> for the sake of not getting bogged down in long arguments over something 
> that was really just a minor point.

Haskell is certainly not a dynamic language in the usual sense of that
term.



More information about the Python-list mailing list