6 ms·
I always feel like functional programming languages start with some math major looking at traditional OOP code and thinking "ITS TOO READABLE!!"
by pelagicAustral 5y ago
I always feel like functional programming languages start with some math major looking at traditional OOP code and thinking "ITS TOO READABLE!!"
- jimsimmons 5y agoI'm confused when people call it simple or elegant. I wonder if the same crowd would use similar words to describe regex.
- gliptic 5y agoCompared to writing the parsing in procedural code? Yes. e.g.: a(b|c)d vs something like: return str.StartsWith("a") && str.EndsWith("d") && str.Length == 3 && (str[1] == "b" || str[1] == "c"); Why wouldn't you consider regular expressions simple and elegant for the problem they are solving?
- parhamn 5y agoMaybe they mean syntax? I guess you could compare it to say syntax like "char 'a' + either char 'b' or char 'c' + char 'd'". I like regex as is but that would def make it more approachable for the folks I know who don't.
- jimsimmons 5y agoThat's a simple example. Things quickly get out of hand once you have a more complex problem like recognising a valid URL [1]: `r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s` Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity". Functional programs also tend to pack many variables or operations in the same line without delimitation. For example Haskell's argument syntax. [1] https://www.geeksforgeeks.org/python-check-url-string/ https://www.geeksforgeeks.org/python-check-url-string/
- SOLAR_FIELDS 5y agoSure, but I think your example goes way too far in the other direction. URLs are quite complex in comparison to a lot of pattern matching problems. Also, what would be the procedural equivalent of the regex you linked? My guess is that if such a solution existed in procedural code, that this symbol soup you linked would be considered simple and elegant in comparison.
- jimsimmons 5y agoProcedural version is more verbose and quite long. That's the downside but it's also the reason why it'd be more readable and maintainable. Procedural version would actually be very similar to a parser combinator with elementary parsers being things like `while str[i].isalphanumeric(): i++`.
- gliptic 5y agoOr you could write it as a PEG (or indeed parser combinators) that would be even easier to read. I don't buy that verbosity on its own makes something readable and maintainable.
- jimsimmons 5y agoImagine comparing diffs of a verbose parser vs diff between two complex regexes. The parser one could be 10x larger or even more, but it'd have a tractable diff. In the regex case I'd have to mentally keep track of column numbers and a bunch of other things.
- gliptic 5y agoPEG doesn't have that problem.
- Zababa 5y ago> Things quickly get out of hand once you have a more complex problem like recognising a valid URL I'm not sure it's so complex. What's the equivalent code for recognising an URL? Regex are implemented in many languages, so this regex is, in a way, a portable library/function in most programming languages. And since it's shared, it can easily be checked way more than any single library would be.
- ReleaseCandidat 5y agoI'd call return str == "abd" || str == "acd" _way_ more elegant and simple, to be honest. Because the actual code would look like return match("a(b|c)d", str)
- dekhn 5y agoerr.... regex, which are a part of a larger field of formal language processing, which is elegant (but not simple). https://en.wikipedia.org/wiki/Regular_language https://en.wikipedia.org/wiki/Regular_language The whole area of parsing and grammar is fascinating.
- jimsimmons 5y agoExpressivity is independent of specification. You can specify the same language in multiple ways and some are more readable than others
- dekhn 5y agoDo you know of a more readable regular language parser than regular expressions? News to me.
- chillfox 5y agoI watched a video explaining monads where the presenter explained that simple meant small… So yeah, I think they would call regex simple as well. It honestly never occurred to me before watching that video that someone could mean small when they say simple. I have always understood simple to mean “easy to understand” or “low complexity”.
- pietrrrek 5y agoMy personal experience from working with functional languages at my university is that there is a certain familiarity-floor which has to be reached before you start to see the language as elegant. This doesn't even translate between languages - I've now worked with F#, Haskell, LISP and Erlang and all of them have required some practice before their elegance was visible to me, even though certain solutions seem to have a similar "shape" across these languages.
- dharmaturtle 5y agoKinda the other way around honestly. FP predates OO by decades. If you think of "traditional OOP" as C-style syntax, then you could conceivably think of C-style syntax as an attempt to make FP more readable. Personally, I prefer FP-style (ML in particular) syntax. I think the fact that OOP languages are copying ML features like pattern matching, preference for immutability/call-by-value, and "do-notation" as a testament to FP's power. Side note about syntax, honestly it's just glue. `<$>`, `|>`, `<*>`, and all the other symbols are just boring operations that are used so often that it's preferable to use symbols so programmers can focus on the meat. In other words: functionA <$> functionB |> functionC <*> functionD is easier to read than functionA(map(functionB(pipe(functionC(bind(functionD))))))
- cdrini 5y agoThis is kind of a surprisingly myopic/inflammatory comment to see on HackerNews. Both systems have their pros and cons, and people tend to be biased to prefer the one they learned first or worked extensively with first. And you don't have to choose; most languages nowadays support elements from both paradigms. But honestly there's nothing even to argue, cause the comment doesn't really say anything.
- bayesian_horse 5y agoMost languages I know don't really shine in both paradigms. Python even comes close, but it is shunned in OOP circles because of dynamic typing and loses street-cred in the FP hoods for its syntax. C# feels clunky in both. F# is good in FP, has good interop with OOP, but why would you? It definitely seems easier (or is more popular) to start people on imperative programming styles, because it is easier to make analogies. From there it's a quick jump to OOP.
- ReleaseCandidat 5y ago> It definitely seems easier (or is more popular) to start people on imperative programming styles, because it is easier to make analogies There are simply _way_ more 'imperative' programmers/teachersTtutorials/books/... than 'functional' ones.
- bayesian_horse 5y agoThinking back to my days of starting programming, it was a lot easier to get my head around "do this then that" rather than working in expressions and functional paradigms. When teaching/instructing others, this impression only deepened. It took a long while to even understand why functional programming has any appeal. I know that some university courses start out with scheme or lisp as a first language. But I suspect they get away with it because they have a captive audience and one that is already selected for high math tolerance.
- anoctopus 5y agoPython loses cred in FP circles for being rather opinionated against FP. Map/filter being second-class to list comprehensions, reduce and partial being in functools, and no multi-line lambdas are the obvious/well-known examples of this, but more generally python has first class functions but doesn't make creating and manipulating functions ergonomic.