Reputation: 13
Is there something equivalent to
(\(x : xs) -> (x, xs)) theList
built into the language, to where I could write something like
let (h, t) = headAndTail theList in h : t
?
Upvotes: 1
Views: 159
Reputation: 152837
Plain old pattern bindings fit the bill.
let h:t = theList in h : t
Upvotes: 4
Reputation: 531205
Data.List
provides uncons :: [a] -> Maybe (a, [a])
:
> uncons "foo"
Just ('f', "oo")
If you really want a partial function of type [a] -> (a, [a])
, you can compose with Data.Maybe.fromJust
.
Upvotes: 5