100 likes | 181 Views
600.429 FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES. Dr. John Peterson Western State Colorado University. Quiz. Why would we be interested in a pure language? What is a type in which each constructor has no arguments like? Example?
E N D
600.429FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University
Quiz • Why would we be interested in a pure language? • What is a type in which each constructor has no arguments like? Example? • What is the meaning of the type (a -> Maybe b) -> [a] -> [b]? • Why does Haskell lack control flow constructs?
Quiz • zipWith (+) [1, 2, 3] [3, 1] • map (*2) [1..4] • map ((* 3) . (- 1)) [2, 3]
Old Homework • http://wiki.western.edu/mcis/index.php/429/Week_1
New Homework • Let’s talk BlackJack • What sort of state would this need? • Where would the random numbers be used? • How can we shuffle the deck functionally? • How would the dealer strategy be encoded? • How would you track your winnings over multiple games?
New Homework • Any questions? • Let’s give the user three tries instead of one.
IO in Haskell • Does the word “Monad” scare you? • Why is there IO in the type of main? • What is the purpose of the IO type? • Is monadic programming significantly different from ordinary imperative programming?
The IO Monad Bottom line: IO tags functions that interact with the outside world IO cannot be “removed” from a type The () type is like void in C – it’s a type you use when there’s no value of interest. Some IO primitives: putStr :: String -> IO () getLine :: IO String
Using do notation The do notation provides a way to make the underlying monadic operators more readable. do op -- execute in the monad without resultvar <- op -- execute and bind return val -- return value (empty computation)
Reading from Strings type ReadS a = String -> [(a,String)] reads :: (Read a) => ReadS a