Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 传递的是什么?_Haskell_Syntax - Fatal编程技术网

Haskell 传递的是什么?

Haskell 传递的是什么?,haskell,syntax,Haskell,Syntax,在守则中: oneChar :: Char -> Doc oneChar c = case lookup c simpleEscapes of Just r -> text r Nothing | mustEscape c -> hexEscape c | otherwise -> char c where mustEscape c = c < ' '

在守则中:

oneChar :: Char -> Doc
oneChar c = case lookup c simpleEscapes of
              Just r -> text r
              Nothing | mustEscape c -> hexEscape c
                       | otherwise   -> char c
    where mustEscape c = c < ' ' || c == '\x7f' || c > '\xff'

simpleEscapes :: [(Char, String)]
simpleEscapes = zipWith ch "\b\n\f\r\t\\\"/" "bnfrt\\\"/"
    where ch a b = (a, ['\\',b])
oneChar::Char->Doc
oneChar c=案例查找c simpleEscapes of
只需r->text r
无任何内容|必须退出c->退出c
|否则->字符c
其中mustEscape c=c<''| | c=='\x7f'| | c>'\xff'
SimpleScapes::[(字符,字符串)]
SimpleScapes=zipWith ch“\b\n\f\r\t\\\\”/“bnfrt\\\”/”
其中ch a b=(a,['\\',b])

r没有被传递给oneChar。r从何而来?

查找c simpleEscapes返回一个
可能是字符串的值,该值可以是
Nothing
Just
r
是包含在
Just
中的字符串,由以下行定义:

Just r -> text r

您正在对lookup c simplescapes返回的值使用case语句,它的类型可能是。可能有两个数据构造函数:Just和Nothing。Just data构造函数由一个值参数化,Nothing data构造函数没有参数


因此,在本例中,r是Just data构造函数的形式参数:它是查找返回值中的实际值。

如果您询问标识符的引入位置,则它受
case
语句中的模式匹配约束,与函数定义中的模式匹配绑定标识符
c
的方式相同

任何模式匹配都可以为关联表达式引入新标识符:

(\(Just x) -> x) foo

let (Just x) = foo in x

f (Just x) = x

case foo of 
    Just x -> x

…所有这些都引入了一个名为
x
的新标识符。事实上,它们几乎是等价的,因为编译器将它们全部转换为
case
块。

case关键字引入模式匹配,其形式为
case EXPR(pattern->EXPR)+
。因此,
Just r
是一种模式,它与
查找c simplescapes的结果相匹配。在模式中,可以绑定变量。基本上这意味着,如果
lookup c simplescapes of
返回一个
Just
,那么
r
将绑定到该
Just
中的值,表达式的结果将是
text r