3 ms·
Which makes it rather unfortunate that Haskell is a language where the recommended way of writing a program to e.g. read two numbers from the user, add them, an
by lmm 3y ago
Which makes it rather unfortunate that Haskell is a language where the recommended way of writing a program to e.g. read two numbers from the user, add them, and print the result, demands understanding "Monad".
- tome 3y agoWhere do I have to 'understand "Monad"' to write this? main = do x <- readLn y <- readLn print (x + y) Do I have to 'understand "Monad"' to write this? def main(): x = input() y = input() print(x + y)
- lmm 3y ago> Where do I have to 'understand "Monad"' to write this? The recommended explanation of what <- is/does will be about "Monad".
- tome 3y agoYou're moving the goalposts! You said 'the recommended way of writing a program to e.g. read two numbers from the user, add them, and print the result, demands understanding "Monad".'
- Tainnor 3y agoI don't think it's "moving the goalposts" when you demand that you can understand, at least to a first-order approximation, what the code that you write is doing. Yes, you could just treat IO as a black box and memorize a bunch of weird syntax rules, but you're going to have a hard time very quickly, especially when you change something and the compiler throws errors at you. FWIW, I think that monads are a powerful abstraction, but they're certainly not incredibly natural and Haskell basically just throws them at you from day 1.
- lmm 3y agoThe recommended way of writing a program in most languages demands some level of understanding what you're writing. You would have to somehow come up with the "<-" parts of that program, and the recommended way of doing that is to understand Monad.
- tome 3y ago> The recommended way of writing a program in most languages demands some level of understanding what you're writing Sure, some. I think this discussion is about exactly what and how much, when it comes to Haskell. > the recommended way of doing that is to understand Monad. That doesn't ring true to me, not from how I learned Haskell and not by analogy with how I learned other languages. I don't remember having to "understand Monad" to write in "do" notation. Quite the opposite in fact. I vividly remember trying to "understand Monad" and being unable to, and then just giving up and trying to use do notation, finding it easy, completely unhelped by what I was trying to "understand", and regretting having spent all the time "understanding". But it's been a long time since I learned a language so maybe I'm mistaken. I would appreciate the point of view of others who have fresher eyes. I will concede that people have a harder time learning Haskell than Python (although I don't think anyone understands the exact reasons) but you gave a very specific example of a program that is supposedly requires some deep understanding to write in Haskell. I wanted to give a counterpoint so people can decide for themselves. That's as far as my claim goes.
- itishappy 3y ago> Where do I have to 'understand "Monad"' to write this? Literally every line? Your example makes it seem so simple! "Do" for function declarations, "<-" for variable assignment... Haskell is just like python! Alright, here I go! fact n = do n' <- n - 1 if n <= 1 then 1 else n * fact n' Uh oh... Couldn't match type `t' with `m t' Expected: t -> m t Actual: m t -> m t What the heck is "m t"?
- bPspGiJT8Y 3y agofact n = do let n' = n - 1 if n <= 1 then 1 else n * fact n' Here you go. Not sure about Haskell but in PureScript it compiles. Use "<-" for functions which return a value in IO type constructor, otherwise use "let".
- deleted 3y ago[deleted]
- itishappy 3y agoThat's kinda the point though, right? There's something different about this `IO` thing that uses special syntax. You might be able to guess something and get it to compile, but should you? fact n = do let n' = do n - 1 if do n <= 1 then do 1 else do n * fact n'
- tome 3y ago> What the heck is "m t"? It's worse than that! What the heck is "->"? But I don't particularly buy the argument. Alright, here I go: def main(): x = input y = input print(x + y) Uh oh... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'builtin_function_or_method' What the heck is "Traceback", "<stdin>", "<module>", "TypeError", "operand", "builtin_function_or_method"? Did I have to understand all those to write the simple Python program? The claim that you have to understand everything about a failure case in order to write simple programs doesn't seem valid to me. It doesn't resonate with how I've learned languages before. However, I may not have the best perspective because I'm very familiar with both Python and Haskell so I don't have the "beginners mind" any more. I would be interested to hear from someone who's a beginner (perhaps in both languages) whether they think the Haskell program requires more understanding than the Python one https://news.ycombinator.com/item?id=37402253 https://news.ycombinator.com/item?id=37402253
- Tainnor 3y agoSure, but if it's just for pedagogy reasons, there's plenty of things you can do in order to learn about HOFs, the type system, typeclasses, Monoid, Functor, etc. before reading in some input, and if that becomes necessary, a teacher can always provide some template with a disclaimer "don't worry about it, we'll cover that later" - same as they do it for Java that forces you to learn what classes are before you can write a program. OTOH, the promise of Haskell is that while it takes longer to learn and getting used to, the payoff is that you can get rid of certain classes of bugs. Whether that's true or not is a different matter entirely, but a priori just because something is hard to learn it doesn't mean it's not worth it.