From 10903c741ab1fdc1761d395d9204a2701843c8c5 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Mon, 29 Feb 2016 20:21:55 +0530 Subject: [PATCH] Code for 29 feb. --- 2016-02-29/DataTypes.hs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 2016-02-29/DataTypes.hs diff --git a/2016-02-29/DataTypes.hs b/2016-02-29/DataTypes.hs new file mode 100644 index 0000000..513149e --- /dev/null +++ b/2016-02-29/DataTypes.hs @@ -0,0 +1,47 @@ +module DataTypes where + +data User = NormalUser { userName :: String + , userAge :: Int } + | Admin { userName :: String + , userAge :: Int + , adminRole :: String } + deriving (Show) + +data FileAccessMode = Read | Write | Append + +data Expr = IntLiteral Int + | StringLiteral String + | Addition Expr Expr + +showUser :: User -> String +showUser (NormalUser name age) = + ("Name: " ++ name ++ " Age: " ++ show age) +showUser (Admin name age role) = + ("Name: " ++ name ++ " Age: " ++ show age ++ " Role: " ++ role) + +data IntList = EmptyList | List Int IntList + +iNull :: IntList -> Bool +iNull x = case x of { EmptyList -> True; _ -> False } + +iLen :: IntList -> Int +iLen x = case x of { EmptyList -> 0; List i rest -> 1 + iLen rest } + +iSum x = case x of { EmptyList -> 0; List i rest -> i + iSum rest } +iDouble x = case x of { EmptyList -> EmptyList; + List i rest -> iDouble (List (i*2) rest) } +iEven x = case x of { EmptyList -> EmptyList; + List a rest -> if even a then List a (iEven rest) else iEven rest } + +data BST = EmptyNode | Node {left::BST, value::Int, right::BST} deriving (Show) + +inOrder :: BST -> [Int] +inOrder EmptyNode = [] +inOrder (Node l v r) = inOrder l ++ [v] ++ inOrder r + +find :: BST -> Int -> Bool +find EmptyNode _ = False +find n@(Node l v r) x + | x == v = True + | x > v = find r x + | otherwise = find l x