{-# LANGUAGE RebindableSyntax #-}
module NumericPrelude.List.Checked
(take, drop, splitAt, (!!), zipWith,
) where
import qualified Algebra.ToInteger as ToInteger
import Algebra.Ring (one, )
import Algebra.Additive (zero, (-), )
import Data.Tuple.HT (mapFst, )
import qualified NumericPrelude.List as NPList
import NumericPrelude.Base hiding (take, drop, splitAt, length, replicate, (!!), zipWith, )
moduleError :: String -> String -> a
moduleError :: String -> String -> a
moduleError String
name String
msg =
String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"NumericPrelude.List.Left." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg
take :: (ToInteger.C n) => n -> [a] -> [a]
take :: n -> [a] -> [a]
take n
n =
if n
nn -> n -> Bool
forall a. Ord a => a -> a -> Bool
<=n
forall a. C a => a
zero
then [a] -> [a] -> [a]
forall a b. a -> b -> a
const []
else \[a]
xt ->
case [a]
xt of
[] -> String -> String -> [a]
forall a. String -> String -> a
moduleError String
"take" String
"index out of range"
(a
x:[a]
xs) -> a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: n -> [a] -> [a]
forall n a. C n => n -> [a] -> [a]
take (n
nn -> n -> n
forall a. C a => a -> a -> a
-n
forall a. C a => a
one) [a]
xs
drop :: (ToInteger.C n) => n -> [a] -> [a]
drop :: n -> [a] -> [a]
drop n
n =
if n
nn -> n -> Bool
forall a. Ord a => a -> a -> Bool
<=n
forall a. C a => a
zero
then [a] -> [a]
forall a. a -> a
id
else \[a]
xt ->
case [a]
xt of
[] -> String -> String -> [a]
forall a. String -> String -> a
moduleError String
"drop" String
"index out of range"
(a
_:[a]
xs) -> n -> [a] -> [a]
forall n a. C n => n -> [a] -> [a]
drop (n
nn -> n -> n
forall a. C a => a -> a -> a
-n
forall a. C a => a
one) [a]
xs
splitAt :: (ToInteger.C n) => n -> [a] -> ([a], [a])
splitAt :: n -> [a] -> ([a], [a])
splitAt n
n [a]
xt =
if n
nn -> n -> Bool
forall a. Ord a => a -> a -> Bool
<=n
forall a. C a => a
zero
then ([], [a]
xt)
else
case [a]
xt of
[] -> String -> String -> ([a], [a])
forall a. String -> String -> a
moduleError String
"splitAt" String
"index out of range"
(a
x:[a]
xs) -> ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ n -> [a] -> ([a], [a])
forall n a. C n => n -> [a] -> ([a], [a])
splitAt (n
nn -> n -> n
forall a. C a => a -> a -> a
-n
forall a. C a => a
one) [a]
xs
(!!) :: (ToInteger.C n) => [a] -> n -> a
!! :: [a] -> n -> a
(!!) [] n
_ = String -> String -> a
forall a. String -> String -> a
moduleError String
"(!!)" String
"index out of range"
(!!) (a
x:[a]
xs) n
n =
if n
nn -> n -> Bool
forall a. Ord a => a -> a -> Bool
<=n
forall a. C a => a
zero
then a
x
else [a] -> n -> a
forall n a. C n => [a] -> n -> a
(!!) [a]
xs (n
nn -> n -> n
forall a. C a => a -> a -> a
-n
forall a. C a => a
one)
zipWith
:: (a -> b -> c)
-> [a]
-> [b]
-> [c]
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith = (a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
NPList.zipWithChecked