diff options
author | William Carroll <wpcarro@gmail.com> | 2020-07-16T22·50+0100 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-07-17T10·39+0100 |
commit | 0f1146cc4cabd9fda4d270e0f91bfb98a5d75554 (patch) | |
tree | a5a303d554f988805ff38003a9f3b688a6cd3197 /scratch | |
parent | feb74b3091be31700b20d868bfd63e709b1c65a3 (diff) |
Partially complete the "Basic Libraries" chapter exercises
I was instructed to benchmark these functions, but I couldn't get the benchmarking library to run using Nix -- although I'm *sure* it's possible. Unfortunately the book recommends using `stack`, which I couldn't reproduce.
Diffstat (limited to 'scratch')
-rw-r--r-- | scratch/haskell-programming-from-first-principles/basic-libraries.hs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/scratch/haskell-programming-from-first-principles/basic-libraries.hs b/scratch/haskell-programming-from-first-principles/basic-libraries.hs new file mode 100644 index 000000000000..bb1f89987e29 --- /dev/null +++ b/scratch/haskell-programming-from-first-principles/basic-libraries.hs @@ -0,0 +1,60 @@ +module BasicLibrariesScratch where + +import Data.Function ((&)) + +-------------------------------------------------------------------------------- +newtype DList a = DL { unDL :: [a] -> [a] } + +instance (Show a) => Show (DList a) where + show (DL x) = "DL " ++ show (x []) + +-- | Create an empty difference list. +emptyDList :: DList a +emptyDList = DL $ \xs -> xs +{-# INLINE emptyDList #-} + +-- | Create a difference list with `x` as the only member. +singleton :: a -> DList a +singleton x = DL $ \xs -> x : xs +{-# INLINE singleton #-} + +-- | Convert the DList into a list. +toList :: DList a -> [a] +toList (DL unDL) = unDL mempty +{-# INLINE toList #-} + +-- | Add an element to the end of a DList. +infixr `snoc` +snoc :: a -> DList a -> DList a +snoc x (DL xs) = DL $ \ys -> xs (x : ys) +{-# INLINE snoc #-} + +-- | Add an element to the beginning of a DList. +infixr `cons` +cons :: a -> DList a -> DList a +cons x (DL xs) = DL $ \ys -> x : xs ys +{-# INLINE cons #-} + +-- | Combine two DLists together. +append :: DList a -> DList a -> DList a +append (DL xs) (DL ys) = DL $ \zs -> zs & ys & xs +{-# INLINE append #-} + +-------------------------------------------------------------------------------- +data Queue a = + Queue { one :: [a] + , two :: [a] + } deriving (Show, Eq) + +emptyQueue :: Queue a +emptyQueue = Queue mempty mempty + +enqueue :: a -> Queue a -> Queue a +enqueue x (Queue en de) = Queue (x:en) de + +dequeue :: Queue a -> Maybe (a, Queue a) +dequeue (Queue [] []) = Nothing +dequeue (Queue en []) = + let (d:de) = reverse en + in Just (d, Queue de []) +dequeue (Queue en (d:de)) = Just (d, Queue en de) |