4 ms·
Write a program that anyone can read. Haskell inevitably ends up looking more like a mathematical equation than the simpler constructs people are more used to f
by shakna 9y ago
Write a program that anyone can read. Haskell inevitably ends up looking more like a mathematical equation than the simpler constructs people are more used to finding in languages like Clojure.
- tome 9y agoThat may or may not be so (my contention is that it is not so) but that's another debate. The question I'm interested in finding the answer to is specifically what important things you can do in Clojure but not Haskell because of Haskell's static types.
- shakna 9y agoWhat can I do in Haskell that I can't do in Clojure, because of Clojure's dynamic typing? If you want to add static typing, you're looking for a contract library. Some are runtime, some use the macro system to get compile-time checking. (In fact, core.typed looks almost exactly like Haskell type definitions.) If you miss Haskell's pattern matching, you can find it at core.match. You want monads instead of variadic functions? Nothing easier. Laziness is a lambda away. Clojure, and so many similar dynamic languages have the power to give you the safety of static typing, if and when you want it. But they don't require it of you. In some cases this can be bad, when you don't think through your data structures enough. In other cases, it can be good because you don't need to think as much as you put things together, and can go back and expand later. I think every language has a time and a place, for the right purpose.
- tome 9y ago> What can I do in Haskell that I can't do in Clojure, because of Clojure's dynamic typing? Also an interesting question, but, again, not equivalent to the one that I asked! joncampbelldev very specifically said that Clojure gives him more flexibility than Haskell. I'm asking for specific examples, otherwise how else can I improve Haskell or its documentation to be more appealing to fans of dynamic languages?
- shakna 9y agoAh, I've said too much in such a debated area, and clumsily. Dynamic types give their power in what they can change. I do not know how easily this can be done with Haskell, but I expect a different pattern, or a more convoluted approach would be needed, but something like this: (defun greet ([:bob] "Hey bob") ([x] (str "Greetings, " x))) So far, something familiar. But where's the dynamism? Let's grab a macro, so that we can do this: (addmatches! greet :chef-matches {:before :beginning} ([:emeril] "Love the zest") ([:child] "First, we baste the chicken!")) Which we can have later use in other conditionals, as well as the corresponding macro removematches! (These macros come from a library, but are not difficult to implement). Why would one ever want such a thing? It allows for the creation of a self-modifying parser, for one. Removes a lot of boilerplate for another.
- tome 9y agoI'm afraid I've never been able to understand macros. What does this do? Is it something like greet :: String -> String greet "bob" = "Hey bob" greet x = "Greeting, " ++ x addmatches :: (String -> String) -> String -> String addmatches f = \case "emeril" -> "Love the zest" "child" -> "First, we baste the chicken!" x -> f x If so, you'd have to design it slightly differently to do removematches in Haskell, I guess.
- shakna 9y agoWell, it modifies greet in place. And this: greet :: String -> String Wouldn't really be writable by a human, because: x -> f x Can represent any two expressions, for example: (addmatches! greet :chef-matches {:before :beginning} ((list? x) :sym) (comment Boolean -> Symbol) (list (apply greet x)) (comment Function -> Recursive...)) addmatches! isn't a separate function. Basically, at compile time, a macro is given the raw untyped tokens from Clojure's parser, and can run all of Clojure's features against them. The *match! macros don't exist at runtime.
- mannykannot 9y ago> What can I do in Haskell that I can't do in Clojure, because of Clojure's dynamic typing? You are making a false equivalence here. This thread started with the implication that the flexibility of dynamic languages is more useful than anything static typing can give, and questioning the grounds for that is not the same as asserting the opposite.
- shakna 9y agoI question whether one is better than another. If Haskell's type system paths the way for the programmer to implement, innovate, and create, then there is no reason for change. The same for Clojure, or any other. I merely answered a question on Haskell's capabilities with a personal anecdote. Haskell has been hard for the teams I worked with, replacing it with Clojure enabled others to reach their potential. But, as for my personal belief on whether static or dynamic typing is better? Different projects have different requirements, as do the hands that craft them.
- SkyMarshal 9y agoHaskell is very readable if you learn to understand the type declarations, which are nicely set on their own line above every function definition. Haskell allows very general type declarations, but ideally once you've gotten your program running, you go back and make the types more specific. Anyone reading it can then see exactly what the whole codebase does without even reading the functions.
- tome 9y agoI'd be even stronger. Haskell's the most readable language I've ever used, and I was an early (ish) adopter of Python mostly because of readability.
- 43224gg252 9y agoCan you link to a small Haskell code base or single file that you believe is "more readable" than any other language?
- tome 9y agoI'll link to some of my own code, since I know it better than any other https://github.com/tomjaguarpaw/haskell-opaleye/blob/master/src/Opaleye/Internal/PrimQuery.hs https://github.com/tomjaguarpaw/haskell-opaleye/blob/master/... For someone who knows Haskell idioms this is extremely readable compared to functionally equivalant code in another language.
- ice109 9y agothis is like 90% `data` and `case`. show me a language where type declaration and case statements aren't readable.
- tome 9y ago> this is like 90% `data` and `case` Welcome to the world of Haskell! Sum types aren't even available in C, C++, C#, Java, Python, Ruby, JavaScript, ..., so I think you're just proving my point for me.
- mannykannot 9y agoReadability is nice, but it is not the most important thing; I think what matters more is being able to reason about whether it is correct - there's lots of ways code can look about right on a first pass while hiding subtle bugs. I do not know whether Haskell is actually superior in this regard, but it seems to be motivated by the right concerns.