Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no head function for Seq in Haskell

Tags:

haskell

I just discovered Data.Sequence and it seems that there is no head or last function. I know you can pattern match on viewL , or use index 0 etc ... But is there a reason such basic functiosn are not implemented (or I am missing them ) ?

like image 686
mb14 Avatar asked Nov 29 '14 12:11

mb14


1 Answers

Using Prelude.head is usually considered bad practice: partial functions are always something of a danger; code like

if null list then
  ...
 else
  let foo = head list in ...

is often written by beginners but would of course better be expressed

case list of
  [] -> ...
  (foo:_) ->

So in many non-base modules, partial functions are echewed, like in this case. The preferred way is, again, pattern matching – on viewL, as you say.

like image 134
leftaroundabout Avatar answered Nov 09 '22 21:11

leftaroundabout