Home > Software engineering >  Which Haskell construct/statement is equivalent to "switch" of C ? Is it "guards&quo
Which Haskell construct/statement is equivalent to "switch" of C ? Is it "guards&quo

Time:11-29

I am trying to benchmark "switch" statement vs "if-else if" statement in many languages and see which construct has the fastest execution. I was trained in college that "switch" is faster.

So my question regarding Haskell: Which Haskell construct/statement is equivalent to "switch" of C ? Is it "guards" or "case of"?

CodePudding user response:

I don't think you can compare them, as you are comparing a construct of an imperative programming language (C ) with that of an functional programming language (Haskell).

Also it very much depends on the compilers, and how they optimize the code in the first place.

CodePudding user response:

Hmm. case-of is closest in meaning.

Multiple function definitions will generally compile to the same code as a case expression. Guards tend to compile to the same code as an if.

But these two languages are very different. Performance tuning in Haskell programs doesn't look anything like what you'd do in C .

  • Related