【发布时间】:2020-03-27 00:06:16
【问题描述】:
我看到了 Haskell 代码的 sn-p,它递归地将两个列表连接在一起,同时按升序对其进行排序:
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) 这行是做什么的。
【问题讨论】:
-
如果您对
@的使用感到疑惑,请参阅What does the “@” symbol mean in reference to lists in Haskell? -
sn-p 中的间距非常单一且具有误导性。
-
您通常会将左侧写为
merge first@(x:xs) second@(y:ys),因为这样更易读。它类似于merge (x:xs) (y:ys),除了这允许您在右侧使用first作为 x:xs 的快捷方式,并使用second作为 y:ys 的快捷方式。 -
由于函数体中没有使用
first和second,所以可以把first@和second@一起去掉
标签: haskell