Browse Source

Code for 29 feb.

Abhinav Sarkar 4 years ago
parent
commit
10903c741a
1 changed files with 47 additions and 0 deletions
  1. 47
    0
      2016-02-29/DataTypes.hs

+ 47
- 0
2016-02-29/DataTypes.hs View File

@@ -0,0 +1,47 @@
1
+module DataTypes where
2
+
3
+data User = NormalUser { userName :: String
4
+                       , userAge  :: Int }
5
+          | Admin { userName  :: String
6
+                  , userAge   :: Int
7
+                  , adminRole :: String }
8
+          deriving (Show)
9
+
10
+data FileAccessMode = Read | Write | Append
11
+
12
+data Expr = IntLiteral Int
13
+          | StringLiteral String
14
+          | Addition Expr Expr
15
+
16
+showUser :: User -> String
17
+showUser (NormalUser name age) =
18
+  ("Name: " ++ name ++ " Age: " ++ show age)
19
+showUser (Admin name age role) =
20
+  ("Name: " ++ name ++ " Age: " ++ show age ++ " Role: " ++ role)
21
+
22
+data IntList = EmptyList | List Int IntList
23
+
24
+iNull :: IntList -> Bool
25
+iNull x = case x of { EmptyList -> True; _ -> False }
26
+
27
+iLen :: IntList -> Int
28
+iLen x = case x of { EmptyList -> 0; List i rest -> 1 + iLen rest }
29
+
30
+iSum x = case x of { EmptyList -> 0; List i rest -> i + iSum rest }
31
+iDouble x = case x of { EmptyList -> EmptyList;
32
+                        List i rest -> iDouble (List (i*2) rest) }
33
+iEven x = case x of { EmptyList -> EmptyList;
34
+                      List a rest -> if even a then List a (iEven rest) else iEven rest }
35
+
36
+data BST = EmptyNode | Node {left::BST, value::Int, right::BST} deriving (Show)
37
+
38
+inOrder :: BST -> [Int]
39
+inOrder EmptyNode    = []
40
+inOrder (Node l v r) = inOrder l ++ [v] ++ inOrder r
41
+
42
+find :: BST -> Int -> Bool
43
+find EmptyNode _ = False
44
+find n@(Node l v r) x
45
+  | x == v    = True
46
+  | x > v     = find r x
47
+  | otherwise = find l x

Loading…
Cancel
Save