4 ms·
Because of types, basically. You can construct the core data structures of your program and the types of the functions operating on those data structures withou
by pka 10y ago
Because of types, basically. You can construct the core data structures of your program and the types of the functions operating on those data structures without having to actually implement said functions. When the types fit together, i.e. your program is logically consistent, you can start filling in the actual code.
This is much faster than writing a large chunk of code, playing around in the REPL thinking everything is ok, only to realize one week later that you didn't think through some core aspect of your design and then having to refactor a sizeable piece of your codebase.
> The last message I get was something like "Array.Array: Index out of bounds". Program crashed. No idea where the error originated.
It is an unfortunate historical artefact that there are still partial functions (i.e. functions that may crash) in the Haskell base libraries. It is generally advised to avoid such functions, but this is not very clearly communicated to newcomers to Haskell.
For example, Elm, a language largely inspired by Haskell, doesn't have partial functions. This is why it prides itself in "not having any runtime errors in production" (excluding things like compiler bugs or unsafe JavaScript libraries, of course).
- jstimpfle 10y agoThe error was my own. Only I couldn't figure out where exactly. Such errors can't be avoided. I need arrays for performance. If performance is no concern I can use Maps, but I still need indexing ("partial functions") for architectural reasons: separation of concerns - can't make "safe" graphs where everything holds a reference to everything "related". I just don't buy the idea that partial functions can be avoided.
- pka 10y agoI.e. look at how PureScript does it [0]. Haskell could do the same. [0] https://pursuit.purescript.org/packages/purescript-arrays/3.0.1/docs/Data.Array#v:index https://pursuit.purescript.org/packages/purescript-arrays/3....
- jstimpfle 10y agoSure, I can use a Maybe variant, but the consequence would be that I wrap the whole program in a layer of maybes for cases that I "know" shouldn't happen but happened -- because I do make mistakes. Or in other words, an "AssertionMonad". No thanks, I'm perfectly fine with assertion errors (i.e. "Index out of bounds"). I just need help with finding my own errors. Stacktraces are a good thing.
- pka 10y agoI agree, stack traces are a good thing. For the time being, make a helper function: ixWithString :: String -> Int -> Array a -> a check the bounds inside and error out with a custom message when the index is outside the array bounds.
- natec425 10y ago> For example, Elm, a language largely inspired by Haskell, doesn't have partial functions. I may be wrong, but I don't think this is technically true. I don't think Elm has only total functions, but it does avoid a lot of common, error-prone partial functions (e.g. accessing elements from a collection).
- pka 10y agoI am not following Elm closely and may be wrong, but I'd be surprised. The creator (Evan) is really picky about having unsafe features in the language. It's even impossible to upload a package to the official repository containing JavaScript code without prior approval, because the native code may crash!