1
0

Add remaining haskell book exercises

This commit is contained in:
2025-12-11 10:28:11 +01:00
parent 3624c712d7
commit 98329e0a3d
221 changed files with 8033 additions and 2 deletions

View File

@@ -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
]