Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

With Pattern Matching


Hm that’s if expressions.


https://erlang.org/doc/reference_manual/expressions.html#pat...

it's more like the = sign in math mathematics.

2x = 10 we can deduce x = 5

Here's an elixir example where a bunch of "if/else" blocks are refactored to use case statements, and then from case statements to pattern matching.

https://nickjanetakis.com/blog/refactoring-elixir-code-if-co...


No..?

If-expressions is a specialized version that only works on booleans, pattern matching is a more general concept (that of course can be used to implement if-expressions, but so can pure lambda calculus)


I did not mean to imply that there are no if blocks in Erlang/Elixir. Just that I use them a whole lot less. Probably an order of magnitude less.

And you still have to decide where your code goes at some level. The pattern of having two function variants (one which matches an empty list, and one the head/tail) is a different idiom to looping. So while for loops go away, you still traverse sequences. And more generally, a lot of the if(else) clauses I would normally write, now get solved with pattern matching.

With control flow languages, the where the code goes and what the code does feel very intertwined, whereas with the pattern matching idioms, I find that where it goes, and what it does, are more orthogonally declared.


And if statement is just syntax sugar over goto.

After using mostly Erlang for several years, 'if' feels like ugly atavism.


technically, they are case statements. Even the if is just sugar over case.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: