DOC PREVIEW
CALTECH CS 11 - Haskell track

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 11 Haskell track: lecture 6This week:ModulesArraysMore MonadsMonadPlusWrapping upModulesHaskell modules much more conservative than ocaml's module systemMuch of the work of e.g. functors done by type classesConsequently, modules are rather simpleModule examplemodule Tree ( Tree (Leaf, Branch), fringe ) wheredata Tree a = Leaf a | Branch (Tree a) (Tree a) fringe :: Tree a -> [a]fringe (Leaf x) = [x]fringe (Branch left right) = fringe left ++ fringe rightModule examplemodule Tree ( Tree(Leaf, Branch), fringe ) where...This means that this module explicitly exportsthe Tree datatypethe fringe functionnothing elseIf written as:module Tree where ...then everything in module is exportedImporting into modulesmodule Main whereimport Tree ( Tree(Leaf, Branch), fringe )main = print (fringe (Branch (Leaf 1) (Leaf 2)))If the second line was justimport Treethen everything exported from Tree module would be importedAvoiding name clashes (1)By default, imported names dumped into local namespaceWhat if two modules are used which share names?Can explicitly qualify names during importAvoiding name clashes (2)module Main whereimport Tree ( Tree(Leaf, Branch), fringe )import qualified Fringe ( fringe ) Module Fringe contains a function fringe which has same name as Tree module's fringe functionQualifying means refer to second fringe as Fringe.fringeimport qualified ... as ...Can rename the qualifier of a module by using the as syntaximport qualified VeryLongModuleName as VWatch out for this:import Foobar as F Brings in all names from Foobar with and without qualification (why would you want this?)hiding declarationsCan selectively hide some names upon import with a hiding declaration:Assume module A exports x and yimport A -- x and y importedimport A hiding y -- x onlyimport qualified A hiding y -- A.x onlyModules and instancesInstance declarations not explicitly imported/exportedmodules export all instance declarationsArraysHaskell arrays are functionalno in-place update in standard Arraysthough some mutable array types in ghc libraries (not covered here)Arrays require an Ix (indexing) type to represent indices (usually just Int)Array indicesclass (Ord a) => Ix a where range :: (a, a) -> [a] index :: (a, a) -> a -> Int inRange :: (a, a) -> a -> Bool range (0,4) => [0,1,2,3,4] range ((0,0), (1,2)) => [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]Array indicesclass (Ord a) => Ix a where range :: (a, a) -> [a] index :: (a, a) -> a -> Int inRange :: (a, a) -> a -> Bool index (1,9) 2 => 1index ((0,0), (1,2)) (1,1) => 4Creating arraysarray :: (Ix a) => (a,a) -> [(a,b)] -> Array a b squares = array (1,100) [(i, i*i) | i <- [1..100]]Accessing array elementssquares ! 8 => 64 bounds squares => (1,100)Examplefibs :: Int -> Array Int Int fibs n = a where a = array (0, n) ([(0, 1), (1, 1)] ++ [(i, a!(i-2) + a!(i-1)) | i <- [2..n]])Q: why do we need the where clause?"Modifying" array elements(//) :: (Ix a) => Array a b -> [(a,b)] -> Array a bsquares_bad = squares // [(8, 63)]squares_bad ! 8 => 63Creates a new array, not modifying in placeOther ways to actually modify in placebut need to be in e.g. IO monadMonadPlusMany Monads have a notion of a "zero" elementsome kind of "addition" of monadic objectsThis is captured in the MonadPlus classclass Monad m => MonadPlus m where mzero :: m a mplus :: m a -> m a -> m aMonadPlus instancesinstance MonadPlus Maybe wheremzero = NothingNothing `mplus` ys = ysxs `mplus` ys = xsinstance MonadPlus [] wheremzero = []mplus = (++)Where to now? (1)Lots of information on the webwww.haskell.orgwww.haskell.org/ghcHaskell mailing lists:www.haskell.org/haskellwiki/Mailing_Listshaskell mailing listhaskell-cafe mailing listWhere to now? (2)Lots of interesting paper collectionsI particularly recommend Phil Wadler's papers:http://homepages.inf.ed.ac.uk/wadler/ Good examples:"Imperative Functional Programming""Monads for Functional Programming""Comprehending


View Full Document

CALTECH CS 11 - Haskell track

Download Haskell track
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Haskell track and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Haskell track 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?