表达式"first@(x:xs)"和"second@(y:ys)"是什么意思



我看到一段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 first @ (x:xs) second @(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

所以我们可以说:

  1. CCD_ 3接受发送的整个参数
  2. first占据了争论的主导地位;论点的第一个元素
  3. rest接受除参数的第一个元素之外的所有内容

最新更新