3 ms·
I'm a statistician by training, though these days I do a variety of work as an engineer. I'm a huge proponent of static typing, but I have to say that there's
by chimeracoder 11y ago
I'm a statistician by training, though these days I do a variety of work as an engineer.
I'm a huge proponent of static typing, but I have to say that there's a reason that the predominant languages used by statisticians and scientists are generally dynamically-typed (Python, R, MATLAB, etc.) - and it's not that these people simply "don't know how to deal with static types"[0].
I haven't used Haskell in a few years, so it's a bit rusty, but I'll use it as an example, since it's the most widely-known strict static type system. Hindley-Milner type systems like Haskell often don't allow dependent types[1], which means that a 2D vector and 3D vector are the same type unless their dimensions are known at compile-time (which is oftentimes not the case). This is a big problem, because I may want to run a regression on an unknown number of variables, but also constrain the number of variables in alternative regressions to use the same number of variables.
In Haskell, you might solve this problem with a tuple, except then (1) your vector elements may be of the wrong type (bad!), and (2) You can only have vectors of length 63 or less.[2]
By contrast, in MATLAB, it's trivial to declare a number that may also be a vector, but act like a scalar for certain operations, but also be correctly "incompatible" with vectors of incorrect dimensions[3].
Yeah, you can do this in Haskell/Scala/Go/<insert-language-here>. But it's really, really annoying.
There is one assignment that I had in school which was done in MATLAB, and just for fun, I tried reimplementing it in a few other languages (Haskell, Python, Go, and a few others). I ended up giving up in frustration with all three of these, whereas the original MATLAB assignment only took a few hours (most of which was figuring out the math behind it, not writing the code). My Python attempt was the one which came the closest, but even then it was just so hideous that I couldn't bear to work on it any longer[4].
[0] I haven't used Julia, so I can't say whether the optional typing there solves this problem or not.
[1] At least, Haskell didn't as of a couple of years ago, which was the last time I checked. Though it seems this is still an open problem: https://wiki.haskell.org/Dependent_type#Dependent_types_in_Haskell_programming https://wiki.haskell.org/Dependent_type#Dependent_types_in_H...
[2] https://downloads.haskell.org/~ghc/7.0.4/docs/html/libraries/ghc-prim-0.2.0.0/src/GHC-Tuple.html#%28%2C%29 https://downloads.haskell.org/~ghc/7.0.4/docs/html/libraries...
[3] If you think that this sounds easy in your language, try generalizing this where `v` can be an element in any vector space, not just the sorts of vector spaces that we're used to dealing with as computer scientists.
[4] Yes, I am aware of the irony in saying saying that this Python code was more hideous than the equivalent MATLAB code. MATLAB is awful in so many ways, but at the end of the day, it's a domain-specific language, and it tackles its use case in a way that no general-purpose programming language I've seen has.
- chadzawistowski 11y agoIn response to [0], yes Haskell still lacks dependent types. Fortunately there is research going on. Liquid Haskell allows you to use refinement types to help further constrain things. Anyone who is interested in full dependent typing should look at Idris. http://www.idris-lang.org/ http://www.idris-lang.org/
- dragonwriter 11y ago> I'll use Haskell as an example, since it's the most widely-known strict static type system. Hindley-Milner type systems like Haskell often don't allow dependent types, which means that a 2D vector and 3D vector are the same type unless their dimensions are known at compile-time (which is oftentimes not the case). Implementation of dependent types in Haskell, with the specific example of dimension-parameterized vectors, is addressed in [0]. [0] https://www.fpcomplete.com/user/konn/prove-your-haskell-for-great-safety/dependent-types-in-haskell https://www.fpcomplete.com/user/konn/prove-your-haskell-for-...
- GFK_of_xmaspast 11y agoWhat was the problem in [4]? I'll agree that there are a couple nice things about the core matlab syntax, but things tend to get very terrible very quickly once you start to actually do anything with it.
- marvy 11y agoNow I'm curious: what was this problem? I can't imagine any program in Matlab that one can't simply transcribe into Python almost mechanically.
- gugagore 11y agoHere is some stuff written on Julia: https://github.com/JeffBezanson/phdthesis/commit/9832392cce6b5bc7d96697848056d080422d1be8#diff-04b18d435ac2189f459c00bc9ab03732R613 https://github.com/JeffBezanson/phdthesis/commit/9832392cce6...