3 ms·
Yes, you can implement your own loops in Haskell and OCaml (and Scheme and even the GCC-variant of C). However, I meant specifically that OCaml is so 'pragmati
by eru 3y ago
Yes, you can implement your own loops in Haskell and OCaml (and Scheme and even the GCC-variant of C).
However, I meant specifically that OCaml is so 'pragmatic' that they have a for-loop as a built-in syntactic element in the language. They also have mutable variables built-in. In eg Haskell those are 'only' available as part of the standard library.
- habitue 3y agoSorry, bad attempt at humor. I knew what you meant, I was just poking fun at forM_ for being bizarre and unergonomic
- tome 3y agoWhat do you find bizarre and unergonomic about it?
- habitue 3y agoIf you're quite used to Haskell, it's nbd. If you're coming from another language with for loops and someone tells you it's a for loop, you might be surprised that you need to use $ and the loop variable comes after the sequence for x in range(10): print(x) forM_ [0..10] $ \x -> putStrLn x
- karmakurtisaani 3y agoI'm convinced the people who came up with Haskell syntax were on some sort of bath salts.
- nequo 3y agoThe reason it looks the way it does is that forM_ is just a function like any other, not a keyword in a dedicated syntactic construct.
- karmakurtisaani 3y agoYep, and then there's the $, \ and PutStrLn. As a non-Haskell programmer none of those mean anything to me.
- nequo 3y ago$ is short for “put everything to the right inside parentheses” and \x -> y defines an anonymous function. Why do you expect to understand the syntax of a language you never studied?
- karmakurtisaani 3y ago> $ is short for “put everything to the right inside parentheses” There's, you know, also the actual parentheses that everyone already knows. > Why do you expect to understand the syntax of a language you never studied? Because most languages have a syntax that you can grasp almost immediately, since they conform to a common syntax, rather than invent everything all over again. Then you can focus on the actual features of the language and are not bogged down by syntax.
- nequo 3y agoIt doesn't take more than two minutes to understand what $ and \x -> y do, and if you start writing out those parentheses, you will also realize why $ is part of idiomatic Haskell.
- tasuki 3y agoYou know, perhaps Haskell isn't the right language for you. That's fine. Why should it conform to your syntactic preferences?
- eru 3y ago> There's, you know, also the actual parentheses that everyone already knows. Those are also available, and are legal syntax in Haskell. > Because most languages have a syntax that you can grasp almost immediately, since they conform to a common syntax, rather than invent everything all over again. I hope you don't count C's `for(something, something; something; something) {` as part of that intuitive bunch? Btw, Haskell didn't re-invent the wheel for most of its syntax: they stuck to the common and already established syntax of the ML family of languages. ML is from 1973. (For comparison, Wikipedia says C is from 1972.)
- tome 3y agoI think what you're complaining about is not syntax, but lack of syntax. There's nothing syntactially special about this "for loop"! What's the benefit of this then? Firstly, less to learn. If you already understand $ and lambdas, then you understand this structure, and can reuse your knowledge in many places, for example withTempFile "foo" $ \tmpFile -> do <do something with tmpFile> flip fmap myList $ \e -> do <do something with each element i> And you can even create your own constructs. You're not limited to the ones that are built in. (The [0..10] is actually syntax, and I think that's quite unfortunate. `range 0 10` would have been just as good.)
- eru 3y ago> (The [0..10] is actually syntax, and I think that's quite unfortunate. `range 0 10` would have been just as good.) Well, Haskell is in fact flexible enough that you can define that range function. But one reason to make it syntax is that you can define things like [0..], which would be between awkward to impossible to do with a single `range` function in Haskell.
- tome 3y agoYes, indeed they are both functions, enumFromTo and enumFrom! https://www.stackage.org/haddock/lts-22.22/base-4.18.2.1/Prelude.html#v:enumFrom https://www.stackage.org/haddock/lts-22.22/base-4.18.2.1/Pre... https://www.stackage.org/haddock/lts-22.22/base-4.18.2.1/Prelude.html#v:enumFromTo https://www.stackage.org/haddock/lts-22.22/base-4.18.2.1/Pre...
- eru 3y agoYou don't need the $ anymore with `BlockArguments`. But overall, Haskell isn't really any worse here than Rust's for-each; or even C's weird for-loop (where you just have to rote learn what the three parts in the header of the loop do). I agree it's worse than Python's or Rust's vanilla for-loop. I mostly like Python's syntax, I just wish they would make their would declare their indentation syntax to be sugar for a curly brace syntax (like Haskell does), and their would make their statements and blocks give values, like Haskell and Rust do. (Of course, I have some other things I don't like in Python. But her syntax is by-and-large fine.)