News for hackers. Stuff that matters.
Re:Core Erlang by Example by patrec on Monday May 07, 2018 @04:58PM (#17016439)
The flipside is that in Erlang often need repetitive code like so:

      case X of
         {expense, N} -> Total - N;
         {invoice, N} -> Total - N;
         {loss, N}    -> Total - N;
         {credit, N}  -> Total + N
       end
         
whereas in ocaml (and F#, modulo details) you could just do something like:

      match x with 
          | (`expense, n) | (`invoice, n) | (`loss, n) -> total - n
          | (`credit, n) -> total + n;;
The examples are unidiomatic and artificial and you can sometimes ameliorate it with guards, but it's a frequent (minor) annoyance.
Re:Core Erlang by Example by marci on Tuesday May 08, 2018 @05:55AM (#17019855)
Close enough?

  case X of
    {Cat, N} when Cat == expense; Cat == invoice; Cat == loss  -> Total - N;
    {credit, N}  -> Total + N
  end
Re:Core Erlang by Example by klibertp on Tuesday May 08, 2018 @02:33PM (#17023430)
patrec said that

> you can sometimes ameliorate it with guards

which is exactly what you did, so I don't think you're disagreeing here :)

Re:Core Erlang by Example by rdtsc on Monday May 07, 2018 @11:21PM (#17018572)
That's a neat pattern in F#, I like it!

In Erlang's case (pun intended!) I'd probably go for a function though:

    total({expense, N}, Total) -> Total - N;
    total({invoice, N}, Total) -> Total - N;
    total({loss, N},    Total) -> Total - N;
    total({credit, N},  Total) -> Total + N.
Re:Core Erlang by Example by klibertp on Tuesday May 08, 2018 @05:38AM (#17019812)
That still repeats the `Total - N` 3 times, though. As patrec said, it's possible to work around this with guards in this specific case, but in general this kind of duplication is indeed common in Erlang code. You can avoid it many cases by correctly factoring the code, but it's not always possible and then it's a bit irritating. Not too much, though. It's basically the difference between fall-through and non-fall-through `switch` statement: OCaml/F# allow fall-through (as an option, not by default, thankfully), Erlang/Elixir/LFE don't.