Haskell:单个函数中的多个个案陈述 [英] Haskell: Multiple Case Statements in Single Function

查看:143
本文介绍了Haskell:单个函数中的多个个案陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个Haskell函数中包含多个case语句(参见下面的假设函数示例)。



然而,这不是合法的Haskell 。什么是完成同样事情的更好方法?此外,如果case语句没有返回任何内容,而只是设置一些值,为什么在一个函数中有多个case语句是不合法的?



(I在第5行输入'case'会得到一个解析错误)

  tester xy = 
case(x
的真值< 0)真 - > 小于零。
False - > 大于或等于零。
case(y ==foo)
True - > 这个名字是foo。
False - > 这个名字不是富。

请注意,如果我的函数是简单的:

  tester xy = 
case(x <0)
True - > 小于零。
False - > 大于或等于零。

...然后它会编译。

解决方案

一般而言,函数的主体必须是单一表达式(通常由较小的表达式组成)。以下是不允许的,例如:

  fxy = 
foo
bar

这相当于您的第一个示例 - 我们只是将一种表达式(字符串文本)对于另一个(您的case表达式)。

在Haskell函数中包含多个case表达式当然是可能的:

  tester :: Int  - >字符串 - > (字符串,字符串)
tester x y =(a,b)
其中
a =
的情况(x <0)True - > 小于零。
False - > 大于或等于零。
b =
的情况(y ==foo)True - > 这个名字是foo。
False - > 这个名字不是富。

甚至:

  tester :: Int  - >字符串 - > IO()
tester x y = do
putStrLn $ case(x <0)
True - > 小于零。
False - > 大于或等于零。
putStrLn $ case(y ==foo)
True - > 这个名字是foo。
False - > 这个名字不是富。

这些工作是因为函数的主体是单个表达式(尽管这两者都不是真正的惯用Haskell)。

I want to include more than one case statement in a Haskell function (see below for an example of a hypothetical function).

However, it is not legal Haskell. What is a better way of accomplishing the same thing? Furthermore, if the case statements are not returning anything, but simply setting some value, why is it not legal to have more than one case statement in a function?

(I would get a "parse error on input `case'" on line 5)

tester x y =  
   case (x < 0) of  
       True -> "less than zero."  
       False -> "greater than or equal to zero."  
   case (y == "foo")  
       True -> "the name is foo."  
       False -> "the name is not foo." 

Note that if my function were simply:

tester x y =  
   case (x < 0) of  
       True -> "less than zero."  
       False -> "greater than or equal to zero."

...then it would compile.

解决方案

In general the body of a function has to be a single expression (very often made up of smaller expressions). The following isn't allowed, for example:

f x y =
  "foo"
  "bar"

This is equivalent to your first example—we've just substituted one kind of expression (string literals) for another (your case expressions).

It's certainly possible to include more than one case expression in a Haskell function:

tester :: Int -> String -> (String, String)
tester x y = (a, b)
  where
    a = case (x < 0) of  
          True -> "less than zero."  
          False -> "greater than or equal to zero."  
    b = case (y == "foo") of
          True -> "the name is foo."  
          False -> "the name is not foo."

Or even:

tester :: Int -> String -> IO ()
tester x y = do
  putStrLn $ case (x < 0) of  
               True -> "less than zero."  
               False -> "greater than or equal to zero."  
  putStrLn $ case (y == "foo") of
               True -> "the name is foo."  
               False -> "the name is not foo."

These work because the body of the function is a single expression (although neither is really idiomatic Haskell).

这篇关于Haskell:单个函数中的多个个案陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆