Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use pattern matching to bind the last element of a list?

Tags:

haskell

Since there is a way to bind the head and tail of a list via pattern matching, I'm wondering if you can use pattern matching to bind the last element of a list?

like image 665
Michael Litchard Avatar asked Sep 27 '11 23:09

Michael Litchard


1 Answers

Yes, you can, using the ViewPatterns extension.

Prelude> :set -XViewPatterns
Prelude> let f (last -> x) = x*2
Prelude> f [1, 2, 3]
6

Note that this pattern will always succeed, though, so you'll probably want to add a pattern for the case where the list is empty, else last will throw an exception.

Prelude> f []
*** Exception: Prelude.last: empty list

Also note that this is just syntactic sugar. Unlike normal pattern matching, this is O(n), since you're still accessing the last element of a singly-linked list. If you need more efficient access, consider using a different data structure such as Data.Sequence, which offers O(1) access to both ends.

like image 72
hammar Avatar answered Sep 22 '22 15:09

hammar