Haskell Chapter 6. Modules  A module defines some functions, types, and type classes  A program is a collection of modules  Module used in GHCi is.

Slides:



Advertisements
Similar presentations
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Advertisements

Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Haskell Chapter 5, Part I. Topics  Higher Order Functions  map, filter  Infinite lists Get out a piece of paper… we’ll be doing lots of tracing.
Operators, Functions and Modules1 Operators and Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Getting Started with Haskell Tim Sheard. Learning a new language Goals – Learn how to design and use data structures – Learn to write programs using the.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Modules.
Lab 07: Caesar Cypher Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
Overview of the Haskell 98 Programming Language
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
© M. Winter COSC 4P41 – Functional Programming Modules in Haskell Using modules to structure a large program has a number of advantages: Parts of.
0 Modules in Haskell Adapted from material by Miran Lipovaca.
Data in Haskell. What is data? Measurements of some kind stored in a computer. – Examples Simple: numbers, text, truth values … Structured: sequences,
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Defining Classes Modules and ADTs CSCE 314 Spring 2016.
Density. Definition Density is the mass of an object in comparison to the volume it occupies.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
1 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Type declarations.
Haskell Chapter 6.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Haskell Chapter 7.
dr Robert Kowalczyk WMiI UŁ
Learning Basic Python Amlan Talukder.
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
8.4 Volume and Areas of Similar Figures
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell.
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Intro to Computer Science CS1510
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
Clojure to Haskell (It’s mostly syntax).
Higher Order Functions
PROGRAMMING IN HASKELL
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Haskell Modules Roshan Gunathilake.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
Records and Type Classes
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Records and Type Classes
Presentation transcript:

Haskell Chapter 6

Modules  A module defines some functions, types, and type classes  A program is a collection of modules  Module used in GHCi is Prelude (you can import into your own files, to get the same environment)  Programming Language issues:  How well does the language help programmers organize their code (to support larger projects)  How does the language support/encourage reuse?  Accessing functionality: challenging for novice programmers (remember getting linker errors?)

Importing  imports must be before function definitions, normally at the top of the file  Each import statement goes on a separate line  Ex:  import Data.List  import Data.List (nub, sort)  What if you have your own function?  import Data.List hiding (nub)  What if same function (e.g., filter) in 2 modules?  import qualified Data.Map  then must specify Data.Map.filter  Must do for every function from module – tedious! so:  import qualified Data.Map as M  now do M.filter

More on modules and imports  To find useful functions:   In GHCi, add imports  :m + Data.List  :m + Data.List Data.Map Data.Set  After load file  :browse to get a list of functions

Some Examples  Use Data.List functions to count how many times each word appears in a string  Use Data.List functions to see if one list is wholly contained in another  Use Data.Char functions to encode using a Caesar cipher  Book has more

Count Words  Uses words, group, and sort from Data.List import Data.List -- function composition (.) in chapter 5 wordNums :: String -> [(String, Int)] wordNums = map (\ws -> (head ws, length ws)). group. sort. Words wordNums' :: String -> [(String, Int)] wordNums' str = map (\ws -> (head ws, length ws)) (group (sort (words str))) Quick Ex: Play with the individual functions on this slide and the next two… Figure out what words, group, sort, isPrefixOf, any, tails, or and chr do Example: type words "hi ho hi ho to work I go" nothing to submit

Needle in Haystack  Uses tails, isPrefixOf and any from Data.List isIn :: (Eq a) => [a] -> [a] -> Bool needle `isIn` haystack = any (needle `isPrefixOf`) (tails haystack)

Caesar Cipher  Uses ord and chr from Data.Char encode :: Int -> String -> String encode offset msg = map (\c -> chr $ ord c + offset) msg  Or use composition encode' :: Int -> String -> String encode' offset msg = map (chr. (+ offset). ord) msg  Can decode as: decode :: Int -> String -> String decode shift msg = encode (negate shift) msg

Create your own Modules  module moduleName  ( list of functions to export)  may also include “helper” functions that are not exported  must be in the same folder as the module that’s importing it

Example module Geometry ( sphereVolume, sphereArea, cubeVolume, cubeArea, cuboidArea, cuboidVolume,) where sphereVolume :: Float -> Float sphereVolume radius = (4.0 / 3.0) * pi * (radius ^3) sphereArea :: Float -> Float sphereArea radius = 4 * pi * (radius ^ 2) cubeVolume :: Float -> Float cubeVolume side = cuboidVolume side side side cubeArea :: Float -> Float cubeArea side = cuboidArea side side side cuboidVolume :: Float -> Float -> Float -> Float cuboidVolume a b c = rectArea a b * c cuboidArea :: Float -> Float -> Float -> Float cuboidArea a b c = rectArea a b * 2 + rectArea a c * 2 + rectArea c b * 2 rectArea :: Float -> Float -> Float rectArea a b = a * b

Modules can also be hierarchical Sphere.hs Cuboid.hs module Geometry.Sphere ( volume, area ) where volume :: Float -> Float volume radius = (4.0 / 3.0) * pi * (radius ^3) area :: Float -> Float area radius = 4 * pi * (radius ^ 2) module Geometry.Cuboid ( volume, area ) where volume :: Float -> Float -> Float -> Float volume a b c = rectArea a b * c area :: Float -> Float -> Float -> Float area a b c = rectArea a b * 2 + rectArea a c * 2 + rectArea c b * 2 rectArea :: Float -> Float -> Float rectArea a b = a * b

Hierarchical modules continued Cube.hs Using module Geometry.Cube ( volume, area ) where import qualified Geometry.Cuboid as Cuboid volume :: Float -> Float volume side = Cuboid.volume side side side area :: Float -> Float area side = Cuboid.area side side side import qualified Geometry.Sphere as Sphere import qualified Geometry.Cuboid as Cuboid import qualified Geometry.Cube as Cube sphereDemo radius = "The area of a sphere with radius " ++ show radius ++ " is " ++ show (Sphere.area radius)