summaryrefslogtreecommitdiff
path: root/Haskell-book/28/DifferenceList/app
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
committerEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
commit98329e0a3dd4f78b5d815ac3896272ec70904901 (patch)
tree80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/28/DifferenceList/app
parent3624c712d72d246f21d4e710cec7c11e052e0326 (diff)
downloadbook-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/28/DifferenceList/app')
-rw-r--r--Haskell-book/28/DifferenceList/app/Main.hs46
1 files changed, 46 insertions, 0 deletions
diff --git a/Haskell-book/28/DifferenceList/app/Main.hs b/Haskell-book/28/DifferenceList/app/Main.hs
new file mode 100644
index 0000000..8ece171
--- /dev/null
+++ b/Haskell-book/28/DifferenceList/app/Main.hs
@@ -0,0 +1,46 @@
+module Main where
+
+import Criterion.Main
+import Data.DList
+import qualified Data.Queue as Q
+import qualified Data.Sequence as S
+
+schlemiel :: Int -> [Int]
+schlemiel i = go i []
+ where go 0 xs = xs
+ go n xs = go (n - 1) ([n] ++ xs)
+
+constructDlist :: Int -> [Int]
+constructDlist i = toList $ go i empty
+ where go 0 xs = xs
+ go n xs = go (n - 1) (singleton n `append` xs)
+
+processQueue :: Int -> Q.Queue Int
+processQueue i = clear $ Q.pop $ fill i Q.empty
+ where fill 0 xs = xs
+ fill n xs = fill (n - 1) (Q.push n xs)
+ clear Nothing = Q.empty
+ clear (Just xs) = clear $ Q.pop $ snd xs
+
+processList :: Int -> [Int]
+processList i = go (schlemiel i)
+ where go [] = []
+ go (x:xs) = xs
+
+processSeq :: Int -> S.Seq Int
+processSeq i = go $ S.fromList $ schlemiel i
+ where go xs = if S.null xs then xs else go (S.deleteAt 0 xs)
+
+main :: IO ()
+main = defaultMain
+ [ bench "concat list" $
+ whnf schlemiel 123456
+ , bench "concat dlist" $
+ whnf constructDlist 123456
+ , bench "process queue" $
+ whnf processQueue 12345
+ , bench "process list" $
+ whnf processList 12345
+ , bench "process sequence" $
+ whnf processSeq 12345
+ ]