开发者

haskell vs python typing

开发者 https://www.devze.com 2022-12-22 03:54 出处:网络
I am looking for example where things in python would be easier to program just because it is dynamically开发者_C百科 typed?

I am looking for example where things in python would be easier to program just because it is dynamically开发者_C百科 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..


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号