150 likes | 505 Views
More Types. Enumerated Types Data types can be defined using the keyword data. data Color = Blue | Green | Yellow | White | Red data Day = Monday | Tuesday | Wednesday | Thursday| Friday | Saturday | Sunday data Month = Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
E N D
More Types Lecture 11 More Types and I/O
Enumerated Types Data types can be defined using the keyword data. data Color = Blue | Green | Yellow | White | Red data Day = Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday data Month = Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec data Season = Spring | Summer| Automn | Winter These are all called enumerated types. Lecture 11 More Types and I/O
Types belong to categories of classes depending on the functions they support. Eq = = , / = Ord >, <, >=, <= Enum a..b Show may be displayed Lecture 11 More Types and I/O
data Day=Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday deriving (Eq,Ord,Enum,Show) weekD::Day->Bool weekD thisDay = elem thisDay[Monday,Tuesday,Wednesday,Thursday,Friday] weekE::Day->Bool weekE thisDay = not (weekD thisDay) Main> weekD Monday True :: Bool (27 reductions, 59 cells) Main> weekE Monday False :: Bool (29 reductions, 61 cells) Main> weekE Saturday True :: Bool (46 reductions, 77 cells) Lecture 11 More Types and I/O
More I/O Lecture 11 More Types and I/O
Haskell provide the type (or I/O action) IO a which is a program that performs some I/O operation and returns a value of type a. There is a type ( ) in Haskell and some I/O operations return this as their result. The type of such actions is: IO ( ) Examples: getLine :: IO String getChar :: IO Char getLine reads a string from the input stream getChar reads a single character from the input stream We may write output using: putStr :: IO ( ) putChar :: IO ( ) Lecture 11 More Types and I/O
Example: heading = " A sample heading and a number: " testWrite n = putStr "Enter y to test string output" ++ if getChar = = 'y' then "\n"++heading ++ show n++"\n" else "" ERROR "d:haskell/io1.hs":2 - Type error in application *** Expression : getChar == 'y' *** Term : getChar *** Type : IO Char *** Does not match : Char Lecture 11 More Types and I/O
The do notation is used with IO actions. It captures the values returned by IO operations which are then passed to actions which follow. putStrLine:: String ->IO () putStrLine line=do putStr line putStr "\n" putInputLine::IO () putInputLine=do lineIn <-getLine putStrLine lineIn Main> putInputLine This is a test. This is a test. :: IO () (205 reductions, 358 cells) Lecture 11 More Types and I/O
revTwoInLns::IO () revTwoInLns= do ln1<-getLine ln2<-getLine putStrLn (reverse ln2) putStrLn (reverse ln1) Main> revTwoInLns Here is line 1. And line 2 is here. .ereh si 2 enil dnA .1 enil si ereH :: IO ()(526 reductions, 932 cells) Lecture 11 More Types and I/O
To write an IO action that does no IO but returns a result, we use the built-in function return: return :: a -> IO a return x Will cause an IO action which does no I/O but returns x. We use return in a do sequence to return a value. The built-in function, return is used to create an action so that an ordinary value such as a character or a boolean value can be returned from within a do sequence. Lecture 11 More Types and I/O
getLin :: IO String getLin = do c <- getChar if c == '\n' then return "" else do l <- getLin return (c:l) putLin= do lin <- getLin putStr lin Main> putLin jkkdjkd djfhjf jkkdjkd djfhjf :: IO () (191 reductions, 332 cells) Lecture 11 More Types and I/O
heading = " A sample heading and a number: " testWrite n = do putStr "Enter y to test string output: " c <- getChar if c == 'y' then putStr ("\n"++heading ++ show n++"\n") else putStr "" Main> testWrite 256 Enter y to test string output: y A sample heading and a number: 256 :: IO () (76 reductions, 248 cells) Main> testWrite 256 Enter y to test string output: n :: IO () (27 reductions, 85 cells) Lecture 11 More Types and I/O
palindrome:: IO ( ) palindrome = do putStr “Enter your string: “ strn <- getLine if palin strn then putStr (“\n”++strn++”: is a palindrome.”) else putStr (“\n”++ strn++”: is not a palindrome”) Lecture 11 More Types and I/O
You can do I/O from and to files using functions from the IO library. To open a file you have to use the path name. The function openFile creates a handle which is used by other functions to do I/O operations on files. The entire contents of a filecan be returned as a single string using: getContents :: Handle -> IO String type FilePath = String --File path names openFile :: FilePath -> IOMode -> IO Handle hClose :: Handle -> IO () data IOMode=ReadMode|WriteMode|AppendMode|ReadWriteMode Lecture 11 More Types and I/O
myFileIO =doputStr “Writing” sourceFileHandle<-getAndOpenFile“Copy from:“ReadMode destFileHandle <- getAndOpenFile “Copy to: “ WriteMode contents <- hGetContentssourceFileHandle hPutStr destFileHandle contents hClose destFileHandle putStr “Written.” getAndOpenFile :: String -> IOMode -> IO Handle Lecture 11 More Types and I/O