views:

489

answers:

3

I am looking for example where things in python would be easier to program just because it is dynamically typed?

I want to compare it with Haskell type system because its static typing doesn't get in the way like c# or java. Can I program in Haskell as I can in python without static typing being a hindrance?

PS: I am a python user and have played around little bit with ML and Haskell.. ... I hope it is clear now..

+7  A: 

Can I program in Haskell as I can in python without static typing being a hindrance

Yes.

To elaborate, I would say the main gotcha will be the use of existential types in Haskell for heterogeneous data structures (regular data structures holding lists of variously typed elements). This often catches OO people used to a top "Object" type. It often catches Lisp/Scheme programmers. But I'm not sure it will matter to a Pythonista.

Try to write some Haskell, and come back when you get a confusing type error.

You should think of static typing as a benefit -- it checks a lot of things for you, and the more you lean on it, the less things you have to test for. In addition, it enables the compiler to make your code much faster.

Don Stewart
+5  A: 

Well for one you can't create a list containing multiple types of values without wrappers (like to get a list that may contain a string or an int, you'd have to create a list of Either Int String and wrap each item in a Left or a Right).

You also can't define a function that may return multiple types of values (like if someCondition then 1 else "this won't compile"), again, without using wrappers.

sepp2k
Is it a good idea to create a list with multiple types in it?
StackUnderflow
@StackUnderflow: Well, I'm a fan of type safety, so I'd say generally not. But there might be cases where it'd make sense to create e.g. a list of showable values (if all you do is print those items, that'd be perfectly safe, but then you could as well just store the string form, so it's maybe not a good example).
sepp2k
Neither python nor Haskell allows lists containing different types, as far as I know.
Michael Daum
@Michael: Try typing `[1, "abc", True]` into `python` and `ghci`. Python allows it, Haskell does not.
Nathan Sanders
But you can of course have a list of type [Foo], where `data Foo = FooString String | FooInt Int | FooBool Bool`.
jrockway
+3  A: 

Like Chris said, this is one objective question (what can a dynamically typed language do that a statically typed one can't?) and one subjective question (can I use Haskell without static typing being a hindrance). So you're going to get mostly subjective answers, because the first question is not as interesting.

For me, the biggest hindrance was Haskell's IO type, because I had to stop and think about what code does I/O and what code doesn't, and explicitly pass information between the two. Everything else was pretty easy. If you commonly write

if someCondition:
    return 1
else:
    return "other"

Then you're making your own problems, Python just doesn't stop you from doing it. Haskell will, and that's about the only difference. The only exception is that this is sort of common in Python:

if someErrorCondition:
    return None
else:
    return NewItem(Success)

You can't do that in Haskell because there is no common None object. But there are easy ways to work around it.

I did find the type errors confusing at first, but I learned to read them in about a week.

I want to echo Don's advice: just try writing some Haskell and come back when you get a confusing type error.

Nathan Sanders
Of course, though, we have the perfectly statically typed: if someError then Nothing else Just aYay for polymorphic data structures.
Don Stewart
@dons: Which is what he meant by "easy ways to work around it," I'm sure. I actually like the Haskell way better because you know when something might be None, as opposed to showing up at random like in most OO languages.
Chuck