{-# LANGUAGE RebindableSyntax #-}{- |
Some functions that are counterparts of functions from "Data.List"
using NumericPrelude.Numeric type classes.
They are distinct in that they check for valid arguments,
e.g. the length argument of 'take' must be at most the length of the input list.
However, since many Haskell programs rely on the absence of such checks,
we did not make these the default implementations
as in "NumericPrelude.List.Generic".
-}moduleNumericPrelude.List.Checked(take,drop,splitAt,(!!),zipWith,)whereimportqualifiedAlgebra.ToIntegerasToInteger-- import qualified Algebra.Ring as RingimportAlgebra.Ring(one,)importAlgebra.Additive(zero,(-),)importData.Tuple.HT(mapFst,)importqualifiedNumericPrelude.ListasNPListimportNumericPrelude.Basehiding(take,drop,splitAt,length,replicate,(!!),zipWith,)moduleError::String->String->amoduleErrornamemsg=error$"NumericPrelude.List.Left."++name++": "++msg{- |
Taken number of elements must be at most the length of the list,
otherwise the end of the list is undefined.
-}take::(ToInteger.Cn)=>n->[a]->[a]taken=ifn<=zerothenconst[]else\xt->casextof[]->moduleError"take""index out of range"(x:xs)->x:take(n-one)xs{- |
Dropped number of elements must be at most the length of the list,
otherwise the end of the list is undefined.
-}drop::(ToInteger.Cn)=>n->[a]->[a]dropn=ifn<=zerothenidelse\xt->casextof[]->moduleError"drop""index out of range"(_:xs)->drop(n-one)xs{- |
Split position must be at most the length of the list,
otherwise the end of the first list and the second list are undefined.
-}splitAt::(ToInteger.Cn)=>n->[a]->([a],[a])splitAtnxt=ifn<=zerothen([],xt)elsecasextof[]->moduleError"splitAt""index out of range"(x:xs)->mapFst(x:)$splitAt(n-one)xs{- |
The index must be smaller than the length of the list,
otherwise the result is undefined.
-}(!!)::(ToInteger.Cn)=>[a]->n->a(!!)[]_=moduleError"(!!)""index out of range"(!!)(x:xs)n=ifn<=zerothenxelse(!!)xs(n-one){- |
Zip two lists which must be of the same length.
This is checked only lazily, that is unequal lengths are detected only
if the list is evaluated completely.
But it is more strict than @zipWithPad undefined f@
since the latter one may succeed on unequal length list if @f@ is lazy.
-}zipWith::(a->b->c){-^ function applied to corresponding elements of the lists -}->[a]->[b]->[c]zipWith=NPList.zipWithChecked