简体   繁体   English

哪个 Haskell 构造/语句等效于 C++ 的“开关”? 是“守卫”还是“案例”?

[英]Which Haskell construct/statement is equivalent to "switch" of C++? Is it "guards" or "case of"?

I am trying to benchmark "switch" statement vs "if-else if" statement in many languages and see which construct has the fastest execution.我正在尝试用多种语言对“switch”语句与“if-else if”语句进行基准测试,看看哪个构造执行速度最快。 I was trained in college that "switch" is faster.我在大学接受过“切换”更快的培训。

So my question regarding Haskell: Which Haskell construct/statement is equivalent to "switch" of C++?所以我关于 Haskell 的问题是:哪个 Haskell 构造/语句相当于 C++ 的“开关”? Is it "guards" or "case of"?是“守卫”还是“案例”?

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).我认为您无法比较它们,因为您正在比较命令式编程语言 (C++) 的构造和函数式编程语言 (Haskell) 的构造。

Also it very much depends on the compilers, and how they optimize the code in the first place.此外,它在很大程度上取决于编译器,以及它们如何优化代码。

Hmm.唔。 case - of is closest in meaning. case - of的意思最接近。

Multiple function definitions will generally compile to the same code as a case expression.多个 function 定义通常会编译为与case表达式相同的代码。 Guards tend to compile to the same code as an if .守卫倾向于编译为与if相同的代码。

But these two languages are very different.但这两种语言非常不同。 Performance tuning in Haskell programs doesn't look anything like what you'd do in C++. Haskell 程序中的性能调整看起来与您在 C++ 中所做的完全不同。

tl;dr there are no real equivalents, but switch is most easily translated with case , and if is most easily translated with guards. tl; dr没有真正的等价物,但switch最容易用case翻译,而if最容易用警卫翻译。

Most often, switching is done without any dedicated construct at all, but simply with multiple function clauses.大多数情况下,切换完全不需要任何专用结构,而只需使用多个 function 子句即可。

f :: Char -> String

...
f 'k' = "kay"
f 'l' = "el"
f 'm' = "em"
...

Which is in fact translated to a case expression by GHC internally,这实际上是由 GHC 在内部翻译成一个case表达式,

f c = case c of
  ...
  'k' -> "kay"
  'l' -> "el"
  'm' -> "em"
  ...

Of course, all of this, like all of Haskell, is purely functional.当然,所有这些,就像所有Haskell 一样,纯粹是功能性的。 It doesn't work like switch in C++, which is all about the side-effects.它不像 C++ 中的switch那样工作,这都是关于副作用的。

...But Haskell can do side-effects too, only, they're not statements but expressions of aa suitable monadic type. ...但是 Haskell 也可以产生副作用,只是它们不是语句,而是合适的单子类型的表达式。 case can thus be used even for imperative switching.因此,甚至可以将case用于命令式切换。

main :: IO ()
main = do
   putStrLn "Hi, what's your name?"
   userName <- getLine
   case words useName of
     [singleName] -> do
        putStrLn "Select if this is your first (F) or last (L) name."
        nameKind <- getLine
        ...
     [firstName, lastName] -> do
        putStrLn "You entered you full name, thanks."

Haskell's if most directly corresponds to C++' ?: ternary operator, rather than its if construct. Haskell 的if最直接对应于 C++' ?:三元运算符,而不是它的if构造。 But again, this can be used for expressions as well as imperative flow, because in Haskell everything is an expression.但同样,这可用于表达式以及命令式流,因为在 Haskell 中,一切都是表达式。

main = do
   putStrLn "Hi, what's your name?"
   userName <- getLine
   if length (words useName) == 2
    then putStrLn "You entered you full name, thanks."
    else error "Please enter both first and last name."

Note that the else is obligatory.请注意, else是强制性的。

if is actually not used very often in Haskell; if实际上在 Haskell 中并不经常使用; guards are more elegant when there are multiple different conditions.当有多种不同的条件时,守卫会更加优雅。

The closest analogue to a C++ if without else (“if cond then do this stuff, else just continue”) is when , which is not a keyword but just a combinator defined in a library.与 C++ if without else最接近的类似物(“if cond then do this stuff, else just continue”)是when ,它不是关键字,而只是库中定义的组合子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM