Skip to content

Commit 81c5e54

Browse files
authored
Upload implementation of Deque, Queue on Haskell
0 parents  commit 81c5e54

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

Deque.hs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Main where
2+
3+
import Prelude hiding (head, tail, last, init)
4+
5+
class Deque q where
6+
empty :: q a
7+
isEmpty :: q a -> Bool
8+
9+
cons :: a -> q a -> q a
10+
head :: q a -> a
11+
tail :: q a -> q a
12+
13+
snoc :: q a -> a -> q a
14+
last :: q a -> a
15+
init :: q a -> q a
16+
17+
data BankersDeque a = BD Int [a] Int [a] deriving (Show)
18+
19+
c = 3
20+
21+
check lenf f lenr r =
22+
if lenf > c*lenr + 1 then
23+
let i = (lenf + lenr) `div` 2
24+
j = lenf + lenr - i
25+
f' = take i f
26+
r' = r ++ reverse(drop i f)
27+
in BD i f' j r'
28+
else if lenr > c*lenf + 1 then
29+
let j = (lenf + lenr) `div` 2
30+
i = lenf + lenr - j
31+
r' = take j r
32+
f' = f ++ reverse(drop j r)
33+
in BD i f' j r'
34+
else BD lenf f lenr r
35+
36+
instance Deque BankersDeque where
37+
empty = BD 0 [] 0 []
38+
39+
isEmpty (BD lenf f lenr r) = (lenr+lenf == 0)
40+
41+
--insert forward
42+
cons x (BD lenf f lenr r) = check(lenf + 1) (x:f) lenr r
43+
44+
head (BD lenf [] lenr r) = error "empty deque"
45+
head (BD lenf (x:f') lenr r) = x
46+
47+
tail (BD lenf [] lenr r) = error "empty deque"
48+
tail (BD lenf (x:f') lenr r) = check(lenf - 1) f' lenr r
49+
50+
--insert back
51+
snoc (BD lenf f lenr r) x = check lenf f (lenr + 1) (x:r)
52+
53+
last (BD lenf f lenr []) = error "empty deque"
54+
last (BD lenf f lenr (x:r')) = x
55+
56+
--first elements
57+
init (BD lenf f lenr []) = error "empty deque"
58+
init (BD lenf f lenr (x:r')) = check lenf f (lenr - 1) r'
59+
60+
main :: IO ()
61+
main = let q = BD 4 [1, 2, 3, 4] 2 [3, 2]
62+
in print $ last q

Examples.hs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{-# LANGUAGE MultiWayIf #-}
2+
3+
module Main where
4+
5+
import Data.List
6+
import Data.Char
7+
8+
{- multiline
9+
comment -}
10+
-- one line comment
11+
12+
--MultiIf
13+
multiIf :: String -> String
14+
multiIf t =
15+
if | t == "Hi" -> "HIII"
16+
| t == "Bye" -> "Bye"
17+
| otherwise -> " Hmmm"
18+
19+
20+
--compairing
21+
multiEx :: String -> String
22+
multiEx t
23+
| t == "Hi" = "HIII"
24+
| t == "Bye" = "Bye"
25+
| otherwise = " Hmmm"
26+
27+
28+
--pattern maching
29+
multiFunc :: String -> String
30+
multiFunc "HI" = "HELLO"
31+
multiFunc "Bo" = "BOOOO"
32+
multiFunc _ = "So good"
33+
--Rem: pattern mstching up -> down
34+
35+
36+
--case .. of
37+
alice :: String -> String
38+
alice pattern =
39+
case pattern of
40+
"HI" -> "Hello, King"
41+
_ -> "Emm...What?"
42+
43+
44+
--let it be in
45+
yourMark :: Int -> Int
46+
yourMark mark =
47+
let florinskiy_question = True
48+
new_mark = 2
49+
in
50+
if | florinskiy_question -> new_mark
51+
| otherwise -> mark
52+
53+
54+
--new types
55+
addToList :: String -> [String] -> [String]
56+
addToList str dict = str : dict
57+
58+
59+
--lambda functions
60+
ten ::[Double]->[Double]
61+
ten = map (\n->n*10)
62+
63+
64+
--your own operator
65+
(<+>) f g = \x -> f (g x)
66+
67+
68+
--composition (map) with lambda
69+
pretty :: [String] -> [String]
70+
pretty = map (stars . big)
71+
where
72+
big = map toUpper
73+
stars = \x -> "* " ++ x ++ " *"
74+
75+
76+
main :: IO ()
77+
main =
78+
print (multiIf "Hi")
79+
80+
--print (multiEx "wow")
81+
82+
--print (multiFunc "a")
83+
84+
--print (alice "HI")
85+
86+
--print . yourMark $ 5
87+
88+
{-print ( "incredible" `addToList` hosts )
89+
where hosts = ["amazing", "wow"]-}
90+
91+
--print . ten $ [1.2, 0.2, 0.99]
92+
93+
{-print (sq2 5)
94+
where sq2 = \x -> x * x-}
95+
96+
{-putStrLn ( head functions "Hi")
97+
where
98+
functions = [ \x -> x ++ " Bro",
99+
\x -> x ++ " Sister"
100+
]-}
101+
102+
--print <+> yourMark $ 5
103+
104+
--print . pretty $ ["c++", "haskell", "python"]
105+
106+
{-let
107+
fvalue = 3.14 / 2
108+
coeffs = [fvalue]
109+
in print $coeffs !! 0 -}

Haskell.ppt

791 KB
Binary file not shown.

Queue.hs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Main where
2+
3+
import Prelude hiding (head, tail)
4+
5+
--typeclass (interface be like)
6+
class Queue q where
7+
empty :: q a
8+
isEmpty :: q a -> Bool
9+
snoc :: q a -> a -> q a
10+
head :: q a -> a
11+
tail :: q a -> q a
12+
13+
--your datatype constructor arguments magic
14+
data BankersQueue a = BQ Int [a] Int [a] deriving (Show)
15+
16+
check lenf f lenr r =
17+
if lenr <= lenr
18+
then BQ lenf f lenr r
19+
else BQ (lenf + lenr) (f ++ reverse r) 0 []
20+
21+
--typeclass implementation
22+
instance Queue BankersQueue where
23+
empty = BQ 0 [] 0 []
24+
isEmpty (BQ lenf f lenr r) = (lenf == 0)
25+
26+
--insert new element
27+
snoc (BQ lenf f lenr r) x = check lenf f (lenr + 1) (x:r)
28+
29+
head (BQ lenf [] lenr r) = error "empty queue"
30+
head (BQ lenf (x:f) lenr r) = x
31+
32+
tail (BQ lenf [] lenr r) = error "empty queue"
33+
tail (BQ lenf (x:f') lenr r) = check (lenf - 1) f' lenr r
34+
35+
36+
main :: IO ()
37+
main = let q = BQ 3 [2, 1, 3] 4 [4, 4, 4, 4]
38+
in --print . head . tail $ snoc q 3
39+
print $ snoc q 3

0 commit comments

Comments
 (0)