Algebraic Data Types

Posted on 2015-08-16 23:42:59 +0900 in Functional Programming Haskell CIS194

Lecture Content

Algebraic data types in general
data AlgDataType = Constr1 Type11 Type12
                 | Constr2 Type21
                 | Constr3 Type31 Type32 Type33
                 | Constr4

This is the general Algebraic data types definition.

One important note:

  • type and data constructor names must always start with a capital letter
  • variables(including names of functions) must always start with a lowercase letter

Home Work

Exercise 1
parseMessage :: String -> LogMessage
parseMessage s = case words s of
  ("I":t:xs) -> LogMessage Info (read t) (unwords xs)
  ("W":t:xs) -> LogMessage Warning (read t) (unwords xs)
  ("E":c:t:xs) -> LogMessage (Error $ read c) (read t) (unwords xs)
  _ -> Unknown s

parse :: String -> [LogMessage]
parse = map parseMessage . lines
Exercise 2
insert :: LogMessage -> MessageTree -> MessageTree
insert (Unknown _) m = m
insert m Leaf = Node Leaf m Leaf
insert m (Node l lg r)
  | mT < logT = Node (insert m l) lg r
  | otherwise = Node l lg (insert m r)
  where
    getTimeStamp x = case x of (LogMessage _ t _)-> t
                               _ -> 0
    mT = getTimeStamp m
    logT = getTimeStamp lg
Exercise 3
build :: [LogMessage] -> MessageTree
build = foldr insert Leaf
Exercise 4
inOrder :: MessageTree -> [LogMessage]
inOrder Leaf = []
inOrder (Node l lg r) = inOrder l ++ [lg] ++ inOrder r
Exercise 5
whatWentWrong :: [LogMessage] -> [String]
whatWentWrong = map getMsg . filter seri . inOrder . build
  where seri (LogMessage (Error e) _ _) = e >= 50
        seri _ = False
        getMsg (LogMessage _ _ m) = m

Test code:

*LogAnalysis> testWhatWentWrong parse whatWentWrong "sample.log" 
["Way too many pickles","Bad pickle-flange interaction detected","Flange failed!"]
----------------------------------- 本文内容遵从CC版权协议转载请注明出自kamelzcs -----------------------------------
«  | Introduction to Haskell »

Hide Comments

comments powered by Disqus