Haskell “first@(x:xs)”和“second@(y:ys)”是什么意思?

Haskell “first@(x:xs)”和“second@(y:ys)”是什么意思?,haskell,Haskell,我看到一段Haskell代码,它递归地将两个列表连接在一起,同时按升序排序: merge :: Ord a => [a] -> [a] -> [a] merge [] xs = xs merge ys[] = ys merge first @ (x:xs) second @(y:ys) | x <y = x :merge xs (y:ys) | otherwise = y : merge ys (x:xs) merge::Ord a=>[a]->[a]->

我看到一段Haskell代码,它递归地将两个列表连接在一起,同时按升序排序:

merge :: Ord a => [a] -> [a] -> [a]
merge [] xs = xs
merge ys[] = ys

merge first @ (x:xs) second @(y:ys)
   | x <y = x :merge xs (y:ys)
   | otherwise = y : merge ys (x:xs)
merge::Ord a=>[a]->[a]->[a]->[a]
合并[]xs=xs
合并ys[]=ys
合并第一个@(x:xs)第二个@(y:ys)

|找到答案的简单方法是在GHCi(GHC解释器)中尝试。如果您运行:

$ ghci
Prelude> functionName bind@(first:rest) = print(bind ++ ", " ++ [first] ++ ", " ++ rest)
Prelude> functionName "test"
"test, t, est"
我们看到,如果调用
functionName
,我们得到的是:

bind  => test
first => t
rest  => est
因此,我们可以说:

  • bind
    接受发送的整个参数
  • 第一个
    是论点的开头;论点的第一个要素
  • rest
    接受除参数的第一个元素之外的所有内容

  • 找到答案的简单方法是在GHCi(GHC解释器)中尝试。如果您运行:

    $ ghci
    Prelude> functionName bind@(first:rest) = print(bind ++ ", " ++ [first] ++ ", " ++ rest)
    Prelude> functionName "test"
    "test, t, est"
    
    我们看到,如果调用
    functionName
    ,我们得到的是:

    bind  => test
    first => t
    rest  => est
    
    因此,我们可以说:

  • bind
    接受发送的整个参数
  • 第一个
    是论点的开头;论点的第一个要素
  • rest
    接受除参数的第一个元素之外的所有内容

  • 如果您想知道
    @
    的用法,请注意代码段中的间距太过单一且容易引起误解。您通常会将左侧写为
    合并第一(x:xs)第二(y:ys)
    ,因为这样更具可读性。它类似于
    merge(x:xs)(y:ys)
    ,只是这允许您在右侧使用
    first
    作为x:xs的快捷方式,使用
    second
    作为y:ys的快捷方式。这是否回答了您的问题?由于函数体中未使用
    first
    second
    ,因此如果您想知道
    @
    的用法,可以将
    first@
    second@
    替换掉,请看,代码段中的间距过于单一且容易引起误解。您通常会将左侧写为
    merge first@(x:xs)second@(y:ys)
    ,因为这样更易于阅读。它类似于
    merge(x:xs)(y:ys)
    ,只是这允许您在右侧使用
    first
    作为x:xs的快捷方式,使用
    second
    作为y:ys的快捷方式。这是否回答了您的问题?由于函数体中未使用
    first
    second
    ,因此您可以将
    first@
    second@
    一起删除