0 Modules in Haskell Adapted from material by Miran Lipovaca.

Slides:



Advertisements
Similar presentations
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Advertisements

15-Jan-15 More Haskell Functions Maybe, Either, List, Set, Map.
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.
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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
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.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
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.
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.
Functions Why we use functions C library functions Creating our own functions.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
0 Functors in Haskell Adapted from material by Miran Lipovaca.
16-Nov-15 More Haskell Functions Maybe, Either, List, Set, Map.
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.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
1 Huffman Codes. 2 ASCII use same size encoding for all characters. Variable length codes can produce shorter messages than fixed length codes Huffman.
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.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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)
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.
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
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
More Haskell Functions
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Haskell.
CSE 3302 Programming Languages
CSE 303 Lecture 14 Strings in C
More Haskell Functions
PROGRAMMING IN HASKELL
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
PROGRAMMING IN HASKELL
Encryption and Decryption
PROGRAMMING IN HASKELL
More Haskell Functions
Type & Typeclass Syntax in function
Lists Part 1 Taken from notes by Dr. Neil Moore
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Haskell Modules Roshan Gunathilake.
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
More Haskell Functions
Records and Type Classes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Records and Type Classes
Presentation transcript:

0 Modules in Haskell Adapted from material by Miran Lipovaca

1 The Haskell prelude So far, we’ve been using built-in functions provided in the Haskell prelude. This is a subset of a larger library that is provided with any installation of Haskell. (Google for Hoogle to see a handy search engine for these.) Examples of other modules: - lists - concurrent programming - complex numbers - char - sets - …

2 Example: Data.List To load a module, we need to import it: numUniques :: (Eq a) => [a] -> Int numUniques = length. nub import Data.List All the functions in this module are immediately available: This is a function in Data.List that removes duplicates from a list. function concatenation

3 You can also load modules from the command prompt: ghci> :m + Data.List Or several at once: import Data.List (nub, sort) import Data.List hiding (nub) ghci> :m + Data.List Data.Map Data.Set Or import only some, or all but some:

4 If duplication of names is an issue, can extend the namespace: import qualified Data.Map Data.Map.filter isUpper $ Data.Map.fromList [(1,'a'),(2,'A'),(3,'b'),(4,'B')] When the Data.Map gets a bit long, we can provide an alias: import qualified Data.Map as M And now we can just type M.filter, and the normal list filter will just be filter.

5 Data.List has a lot more functionality than we’ve seen. A few examples: ghci> intersperse '.' "MONKEY" "M.O.N.K.E.Y" ghci> intersperse 0 [1,2,3,4,5,6] [1,0,2,0,3,0,4,0,5,0,6] ghci> intercalate " " ["hey","there","guys"] "hey there guys" ghci> intercalate [0,0,0] [[1,2,3],[4,5,6], [7,8,9]] [1,2,3,0,0,0,4,5,6,0,0,0,7,8,9]

6 And even more: ghci> transpose [[1,2,3],[4,5,6], [7,8,9]] [[1,4,7],[2,5,8],[3,6,9]] ghci> transpose ["hey","there","guys"] [" htg","ehu","yey","rs","e"] ghci> concat ["foo","bar","car"] "foobarcar" ghci> concat [[3,4,5],[2,3,4],[2,1,1]] [3,4,5,2,3,4,2,1,1]

7 And even more: ghci> and $ map (>4) [5,6,7,8] True ghci> and $ map (==4) [4,4,4,3,4] False ghci> any (==4) [2,3,5,6,1,4] True ghci> all (>4) [6,9,10] True

8 A nice example: adding functions Functions are often represented as vectors: 8x^3 + 5x^2 + x - 1 is [8,5,1,-1]. So we can easily use List functions to add these vectors: ghci> map sum $ transpose [[0,3,5,9], [10,0,0,9],[8,5,1,-1]] [18,8,6,17]

9 There are a ton of these functions, so I could spend all lecture covering just lists. More examples: group, sort, dropWhile, takeWhile, partition, isPrefixOf, find, findIndex, delete, words, insert,… Instead, I’ll make sure to post a link to a good overview of lists on the webpage, in case you need them. In essence, if it’s a useful thing to do to a list, Haskell probably supports it!

10 The Data.Char module: includes a lot of useful functions that will look similar to python, actually. Examples: isAlpha, isLower, isSpace, isDigit, isPunctuation,… ghci> all isAlphaNum "bobby283" True ghci> all isAlphaNum "eddy the fish!"Fal se ghci> groupBy ((==) `on` isSpace) "hey guys its me" ["hey"," ","guys"," ","its"," ","me"]

11 The Data.Char module has a datatype that is a set of comparisons on characters. There is a function called generalCategory that returns the information. (This is a bit like the Ordering type for numbers, which returns LT, EQ, or GT.) ghci> generalCategory ' ' Space ghci> generalCategory 'A' UppercaseLetter ghci> generalCategory 'a' LowercaseLetter ghci> generalCategory '.' OtherPunctuation ghci> generalCategory '9' DecimalNumber ghci> map generalCategory " ¥ t ¥ nA9?|" [Space,Control,Control,UppercaseLetter,DecimalNumber,OtherPunctuation,M athSymbol] ]

12 There are also functions that can convert between Ints and Chars: ghci> map digitToInt "FF85AB" [15,15,8,5,10,11] ghci> intToDigit 15 'f' ghci> intToDigit 5 '5' ghci> chr 97 'a' ghci> map ord "abcdefgh" [97,98,99,100,101,102,103,104]

13 Neat application: Ceasar ciphers A primitive encryption cipher which encodes messages by shifted them a fixed amount in the alphabet. Example: hello with shift of 3 encode :: Int -> String -> String encode shift msg = let ords = map ord msg shifted = map (+ shift) ords in map chr shifted

14 Now to use it: ghci> encode 3 "Heeeeey" "Khhhhh|" ghci> encode 4 "Heeeeey" "Liiiii}" ghci> encode 1 "abcd" "bcde" ghci> encode 5 "Marry Christmas! Ho ho ho!""Rf ww~%Hmwnxyrfx&%Mt%mt%mt&"

15 Decoding just reverses the encoding: decode :: Int -> String -> String decode shift msg = encode (negate shift) msg ghci> encode 3 "Im a little teapot" "Lp#d#olwwoh#whdsrw" ghci> decode 3 "Lp#d#olwwoh#whdsrw" "Im a little teapot" ghci> decode 5. encode 5 $ "This is a sentence " "This is a sentence"

16 Making our own modules We specify our own modules at the beginning of a file. For example, if we had a set of geometry functions: module Geometry ( sphereVolume, sphereArea, cubeVolume, cubeArea, cuboidArea, cuboidVolume ) where

17 Then, we put the functions that the module uses: 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 …

18 Note that we can have “private” helper functions, also: cuboidVolume :: Float -> Float -> Float -> Float cuboidVolume a b c = rectangleArea a b * c cuboidArea :: Float -> Float -> Float -> Float cuboidArea a b c = rectangleArea a b * 2 + rectangl eArea a c * 2 + rectangleArea c b * 2 rectangleArea :: Float -> Float -> Float rectangleArea a b = a * b

19 Can also nest these. Make a folder called Geometry, with 3 files inside it: 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)

20 Can also nest these. Make a folder called Geometry, with 3 files inside it: module Geometry.Cuboid ( volume, area ) where volume :: Float -> Float -> Float -> Float volume a b c = rectangleArea a b * c …