有没有一种干净的方法可以在单个表达式中获取列表的头部和尾部?

Is there a clean way to get the head AND tail of a list in a single expression?

有没有等同于

的东西
(\(x : xs) -> (x, xs)) theList

内置于语言中,我可以在其中编写类似

的内容
let (h, t) = headAndTail theList in h : t

?

Data.List 提供 uncons :: [a] -> Maybe (a, [a]):

> uncons "foo"
Just ('f', "oo")

如果你真的想要 [a] -> (a, [a]) 类型的偏函数,你可以用 Data.Maybe.fromJust.

组合

普通的旧模式绑定符合要求。

let h:t = theList in h : t