Add remaining haskell book exercises
This commit is contained in:
46
Haskell-book/28/DifferenceList/app/Main.hs
Normal file
46
Haskell-book/28/DifferenceList/app/Main.hs
Normal 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
|
||||
]
|
||||
Reference in New Issue
Block a user