I started learning scala a few days ago and when learning it, I am comparing it with other functional programming languages like (Haskell, Erlang) which I had some familiarity with. Does Scala has guard sequences available?
I went through pattern matching in Scala, but is there any concept equivalent to guards with otherwise and all?
Yes, it uses the keyword
if. From the Case Classes section of A Tour of Scala, near the bottom:(This isn’t mentioned on the Pattern Matching page, maybe because the Tour is such a quick overview.)
In Haskell,
otherwiseis actually just a variable bound toTrue. So it doesn’t add any power to the concept of pattern matching. You can get it just by repeating your initial pattern without the guard: