10. haskell Modules

Download 10. haskell Modules

Post on 12-May-2015

211 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

<ul><li>1.ModulesSebastian RettigA Haskell module is aa collection of related functions, A Haskell module is collection of related functions,types and typeclasses. types and typeclasses.</li></ul> <p>2. Functional Programming No Variables Functions only, eventually stored in Modules Behavior do not change, once defined Function called with same parametercalculates always the same result Function definitions (Match Cases) Recursion (Memory) 3. Haskell Features Pure Functional Programming Language Lazy Evaluation Pattern Matching and Guards List Comprehension Type Polymorphism Curried Functions 4. Nice to remember (1)Applicative Context: Maybe, Either for things which can fail [] as non-deterministic result IO as values send or get from outside Applicative functors allows us to operate in applicative typeslike normal types and provide the context! We dont need to pattern match against the Context inevery operation we use in our functions! 5. Nice to remember (2)Typeclasses: define properties of the types an interface for types who have some behavior in common: Eq can be compared Ord can be ordered (&gt;, =, a -&gt; a -&gt; a also in the following way:max :: (Ord a) =&gt; a -&gt; (a -&gt; a) 8. Nice to remember (5)Pointless Style: based on partial Application simpler code maxWithFour x = max 4 x is the same as maxWithFour = max 4 use Function Application ($) to avoid Parenthesis onfunction call with parameters sqrt $ 3 + 4 + 9 use Function Composition (.) to avoid Parenthesis onchaining functions fn = ceiling . negate . tan . cos . max 50 9. Nice to remember (6)Kind: explains the steps which are necessary to evaluate the datafrom that type evaluate = the type is fully applied can be used to find out the parameter-count of a type GHCi- Command :k :k Int returns Int :: * data MyVector4 a b c = Nirvana4 | Single4 {x :: a} | Tuple4 {x :: a, y :: b} | Triple4 {x :: a, y :: b, z :: c}:k MyVector4 returns MyVector4 :: * -&gt; * -&gt; * -&gt; * 10. Nice to remember (7)DO-Notation:main :: IO ()main = doputStrLn Say me your Name!name a) -&gt; a -&gt; [b] -&gt; a foldr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; b scanl :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; [a] scanr :: (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; [b] folding is a general approach for simple recursion scan is like fold, but returns the accumulator state of everyrecursion step instead of the accumulator 12. Modules (1) types and functions are managed in modules main module can load other modules prelude.hs loads mostly used modules on startup import modules at the beginning of a File: import Data.List Selective Import of only some functions of module import Data.List (nub, sort) import only functions nub and sort import Data.List hiding (nub) import all functions but not nub 13. Modules (2) use qualified imports if you have name conflicts often occurs on import modules which are already selective imported by prelude.hs import Data.Map as M use reference to call qualified imports, e.g. M.filter (&gt;5) [3,6,2] 14. Modules (3) create own modules, e.g. File Geometry.hs module Geometry ( sphereVolume , sphereArea ) where //Implementation modules in a namespace must be in same parent Folder, e.g.module Geometry.Sphere( volume, area) where Sphere.hs in folder Geometry 15. Functor Typeclass (1)class Functor f wherefmap :: (a -&gt; b) -&gt; f a -&gt; f b general Interface for things that can be mapped over !!! Functor needs Types with kind * -&gt; * !!! fmap gets a function and a type and maps the functionover the type variable Instance for List:instance Functor [] wherefmap = map Example: fmap (*2) [2,3,4] returns [4,6,8] 16. Functor Typeclass (2)class Functor f wherefmap :: (a -&gt; b) -&gt; f a -&gt; f b Instance for Maybeinstance Functor Maybe wherefmap g (Just x) = Just (g x)fmap g Nothing = Nothing Example: fmap (+3) Nothing returns Nothing fmap (+3) (Just 4) returns (Just 7) 17. Functor Typeclass (3)class Functor f wherefmap :: (a -&gt; b) -&gt; f a -&gt; f b Example: fmap (+3) (Left 4) returns(Left 4) fmap (+3) (Right 4) returns (Right 7) what happens, if we try to do that?fmap (+) (Just 4) lets look at the type::t fmap (+) (Just 4)fmap (+) (Just 4) :: Num a =&gt; Maybe (a -&gt; a) partial application, BUT we can not use the Functor instanceon the result Just (4+) we need an extension Applicative Functors 18. Applicative Functor (1)class (Functor f) =&gt; Applicative f wherepure :: a -&gt; f a() :: f (a -&gt; b) -&gt; f a -&gt; f b pure is a function who wraps a normal value into applicative creates a minimal context () takes a functor with a function in it and another functor extracts that function from the first functor and then maps it over the second one pure f x equals fmap f x specific function exists:() :: (Functor f) =&gt; (a -&gt; b) -&gt; f a -&gt; f bf x = fmap f x 19. Applicative Functor (2) Instance for Maybeinstance Applicative Maybe wherepure = JustNothing _ = Nothing(Just f) something = fmap f something Instance for IOinstance Applicative IO wherepure = returna b = dof f a -&gt; f [a] this is called lifting a function we can predefine a function which gets a Functor andreturns a functor that means, we can bring normal functions inside theWrapper (Context) we lift the function up into the Context 22. Lifting a Function (Applicative) liftA :: Applicative f =&gt; (a -&gt; b) -&gt; f a -&gt; f b same as fmap fmap :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b liftA2 :: (Applicative f) =&gt; (a -&gt; b -&gt; c) -&gt; f a-&gt; f b -&gt; f cliftA2 f a b = f a b gets a function, with 2 parameters operates on 2 Applicatives result is also an Applicative 23. Sources[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/,2012/03/15)[2] The Hugs User-Manual (http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15) </p>